Interview Question

Perl Developer Interview

-

Booking.com

You have datastructure my $users = [ { name => 'John', score => 10, }, { name => 'Bob', score => 1, }, { name => 'Carlos', score => 5 }, { name => 'Alice', score => 5, }, { name => 'Donald', score => 7 } ]; now u have to arrange the name with highest to lower score, if score is same than in alphabetical order #expected output: # John # Donald # Alice # Carlos # Bob

Tags:perl
AnswerAdd Tags

Interview Answers

13 Answers

1

$users = [['name' => 'lucas', 'score' => 10], ['name' => 'pablo', 'score' => 5], ['name' => 'luis', 'score' => 7], ['name' => 'diego', 'score' => 10]]; $res = []; foreach($users as $user) { $res[$user['score']][] = $user['name']; } krsort($res); foreach($res as $score => $users) { if(count($users) > 1) natsort($users); foreach($users as $user) { echo "#".$user." "; } } echo "\n";

Lucas on

0

Can be done easily, if we use a stable sort. I dont know perl; but I am guessing the provided data structure is a hashtable (or a dictionary!) sorted_names = sorted(users.keys()) this will sort all names in alphabetical order Now python's sort is stable; so we can simply finish this by: return sorted(sorted_names, key=lambda x: users[x], reverse=True)

SB on

0

'John', 'score' => 10], ['name' => 'Bob', 'score' => 1], ['name' => 'Carlos', 'score' => 5], ['name' => 'Alice', 'score' => 5], ['name' => 'Donald', 'score' => 7] ]; $list = []; foreach ($users as $user) { $name = $user['name']; $score = $user['score']; if (!isset($list[$score])) { $list[$score] = [$name]; continue; } //User with same count, push the name array_push($list[$score], $name); } krsort($list); foreach ($list as $users) { if (count($users) > 1) { natsort($users); } foreach($users as $user) { echo $user . "\n"; } }

Anonymous on

0

my %hash = () ; foreach (@$users) { $hash{uc $_->{name}} = $_->{score}; } foreach (sort {$hash{$b} $hash{$a} or $a cmp $b} keys %hash ) { print "$_\n"; }

Filippo Biondi on

0

map{ $_->{name } } reverse sort { $a->{score} == $b->{score} ? $b->{name} cmp $a->{name} : $a->{score} $b->{score} } @$users

Alina on

0

In Java: Map map = new HashMap(); for(user : users) { map.put(user.name, user.score); } Set> set = map.entrySet(); List> list = new ArrayList>(set); Collections.sort( list, new Comparator>() { public int compare( Map.Entry o1, Map.Entry o2 ) { return (o2.getValue()).compareTo( o1.getValue() ); } } ); for(Map.Entry entry:list){ System.out.println("# "+entry.getKey()); }

Tabrej on

0

Forgot to mention, if both values are same than sort according to the key.

Tabrej on

0

my $users = [ { name => 'John', score => 10, }, { name => 'Bob', score => 1, }, { name => 'Carlos', score => 5 }, { name => 'Alice', score => 5, }, { name => 'Donald', score => 7 } ]; foreach( sort {$b->{score} $a->{score} || $a->{name} cmp $b->{name}} @$users ) { print $_->{name}, "\n"; }

Arun Venkataraman on

0

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @users = (); @users[0] = {name => 'John', score => 10}; @users[1] = {name => 'Bob', score => 1}; @users[2] = {name => 'Carlos', score => 5}; @users[3] = {name => 'Alice', score => 5}; @users[4] = {name => 'Donald', score => 7}; my @sorted_users = map { $_->{'name'} } sort { $b->{'score'} $a->{'score'} } @users; print Dumper \@sorted_users;

Benoy Bose on

0

print $_->{name}."\n" for (sort { $b->{score} == $a->{score} ? $b->{name} cmp $a->{name} : $b->{score} $a->{score}} @$users);

Kamal Nayan on

0

print $_->{name}."\n" for (sort { $b->{score} == $a->{score} ? lc $a->{name} cmp lc $b->{name} : $b->{score} $a->{score}} @$users); Glassdoor removes comparison operator between $b->{score} $a->{score}. Please add , removing space.

Anonymous on

0

map{ $_->{name } } reverse sort { $a->{score} $b->{score} } @$users

Alina on

0

my %hash = () ; foreach (@$users) { $hash{$_->{name}} = $_->{score}; } foreach (sort {$hash{$b} $hash{$a}} keys %hash ) { print "$_\n"; }

Anonymous on

Add Answers or Comments

To comment on this, Sign In or Sign Up.