Perl Developer Interview Questions

100

Perl Developer interview questions shared by candidates

Top Interview Questions

Sort: Relevance|Popular|Date
Booking.com
Perl Developer was asked...24 March 2014

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

13 Answers

$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"; Less

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) Less

'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"; } } Less

Show more responses
Booking.com

I face only one question. If you are given 2 array A={3,1,2,4} B={1,4}. Write a program to compare two arrays and create another array which holds the common values between two array!

12 Answers

my @A = ( 3,1,2,4,3 ); my @B = ( 1,4,4,5,5 ); my $hash = +{}; my @C; for ( @A ) { $hash->{$_} = 1; } for ( @B ) { $hash->{$_} = 2 if $hash->{$_}; } for ( keys %$hash ) { push @C, $_ if $hash->{$_} > 1; } Less

my @a=(1,2,3,4,8,9,0); my @b=(2,5,6,8); my %a = map{$_ => undef} @a; my @ix = grep { exists( $a{$_} ) } @b; Less

in the previous post was a small error .. correct answer is reposted here #!/usr/bin/perl my @a = ('3','1','2','4','44'); my @b = ('23','44','1','4'); print "@a \n"; print "@b \\n"; my @res; my %a_hash =(); foreach (@a) { ++$a_hash{$_}; }; foreach (@b) { if ($a_hash{$_} >0) {push @res, $_; }; }; print "Common values @res\n"; Less

Show more responses
Booking.com

one question was as under - "Symmetric Difference of Arrays" Input: two arrays of integers Output: one array of integers which occur in only one (not both) arrays Test case: Input: [ 1, 7, 8, 2, 4, 5 ] [ 3, 5, 1, 7, 6, 9 ] Output: [ 8, 2, 4, 3, 6, 9 ]

9 Answers

I answered in Python. I gave them two answers as under def get_diff(a,b): c=[] for i in a: if not(i in b) and not(i in c): c.append(i) for i in b: if not(i in a) and not(i in c): c.append(i) return c or set.union(set(a),set(b))-set.intersection(set(a),set(b)) Less

In java: Integer[] input = new Integer[] {1, 7, 8, 2, 4, 5}; Integer[] input2 = new Integer[] {3, 5, 1, 7, 6, 9}; Set set = new HashSet(Arrays.asList(input)); Set set2 = new HashSet(Arrays.asList(input2)); Set intersection = new HashSet(set); intersection.retainAll(set2); set.removeAll(intersection); set2.removeAll(intersection); set.addAll(set2); Integer[] uniques = new Integer[set.size()]; set.toArray(uniques); Less

use strict; use warnings; use Data::Dumper qw(Dumper); my @a1 = (1, 7, 8, 2, 4, 5); my @a2 = (3, 5, 1, 7, 6, 9); my @r = (); for my $item (@a1) { if(!grep {$_ == $item} @a2) { push @r, $item; } } for my $item (@a2) { if(!grep {$_ ==$item} @a1) { push @r, $item; } } print Dumper \@r; Less

Show more responses
Booking.com

How do you remove duplicates from an array of integers?

7 Answers

my @input = (1,2,3,1,10,4,3,4,5,2,3,4); my $seen = {} my @result = grep { not $seen->{$_} } @input; Less

hi

aliissa

Show more responses
Booking.com

You have the file with word at a single line. #input sample file abactor abaculus abacus Abadite . . Zyrenian #Output ******************a **********b ************************c a) you have to count the character and create a histogram in alphabetical order. b) now you have to produce a histogram with max 80 character in line in reference to max count c) now same out based histrogram based on the character count

6 Answers

#!/usr/bin/env perl use strict; use warnings; &main(); sub main { my %hist; open (my $fh, ") { my $line = lc($_); @arr = split("", $line); foreach my $ch (@arr) { $hist{$ch}++; $char_counter++; $most = $hist{$ch} if $most = $most-$a) { print "x "; } else { print " "; } } print "\n"; } print " a b c d e f g h i j k l m n o p q r s t u v w x y z\n"; } Less

#!/bin/perl # # # # my %tab; while() { chomp; map { ++$tab{$_} } split // ; } foreach my $key (sort keys %tab) { print '*' x $tab{$key}; print "$key\n"; } __DATA__ abactor abadite abaculus abacus Less

#!/bin/perl # # All three conditions a,b,c is here. # use strict; use warnings; open my $fh, 'input.txt' or die('Cannot open file.'); my %counter; my $max = 0; while () { map { $max = ++$counter{$_} > $max ? $counter{$_} : $max } grep { $_ =~ /[a-z]/ } split //, lc $_; } foreach my $k ( reverse sort { $counter{$a} $counter{$b} } keys %counter ) { print ( ('*' x int( 79 / $max * $counter{$k} ) ) . $k . ($counter{$k} == $max ? '' : "\n")); } print "\n"; close($fh); Less

Show more responses
Booking.com

Write a function get_hops_from(page1, page2) that will determine the number of hyperlinks that you would need to click on to get from some page1 on the web to some other page2 on the web. For example, if each page below links to the pages that are indented below it, e.g. page 1 links to pages 2 and 5, and page 2 links to pages 3 and 4, and page 5 links to pages 3 and 7, then the get_hops_from(page1, page7) should return 2 (2 hops), since you have to hop once from page 1 to 5 and once more from page 5 to page 7. page1 : distance == 0 page2 : distance == 1 page3 : distance == 2 page4 : distance == 2 page5 : distance == 2 page3 : distance == 2 page7 : distance == 2 Assume that an API is available to: * get_links(a_page) will return an array/list of all pages that a_page links to

5 Answers

The above answer will perform very poorly since you are using a Depth First Search. In the world wide web the depth of your graph can be practically infinite therefore you should have used BFS (breadth first search) Less

/** * Notes: * ------ * This solution assumes that there must be a sequence of hops from p1 to p2. * BFS uses a large amount of memory because we have to store the pointers. */ public static int getHopsFrom(int p1, int p2) { Queue queue = new LinkedList(); ArrayList pages = getPages(p1); HashSet visitedPages = new HashSet(); visitedPages.add(p1); queue.addAll(pages); int levelSize = pages.size(); int newLevelSize = 0; int hops = 1 ; while(!queue.isEmpty()) { int currentPage = queue.remove(); if(currentPage==p2) { return hops; } else { if(!visitedPages.contains(currentPage)) { queue.addAll(getPages(currentPage)); newLevelSize+= getPages(currentPage).size(); visitedPages.add(currentPage); } } levelSize--; if(levelSize == 0) { hops++; levelSize = newLevelSize ; newLevelSize = 0; } } return -1; } Less

public static int GetHops(int p1, int p2) { Queue currentLevel = new Queue(); Queue nextLevel = new Queue(); int hops = 0; HashSet visited = new HashSet(); currentLevel.Enqueue(p1); while (currentLevel.Count > 0) { int temp = currentLevel.Dequeue(); visited.Add(temp); if (temp == p2) { return hops; } List pages = GetPage(temp); foreach (int item in pages) { if (!visited.Contains(item)) { nextLevel.Enqueue(item); } } if (currentLevel.Count == 0) { currentLevel = new Queue(nextLevel); nextLevel.Clear(); hops++; } } return -1; } Less

Show more responses
Booking.com

Can you optimize the algoritm which you wrote.

5 Answers

With hash table is fastest

sub anagram_part { my ( $str, $substr ) = @_; my @str = split //, $str; my @substr = split //, $substr; L: while ( @str && @substr ) { my $cl = shift @substr; while ( my $c = shift @str ) { goto L if $cl eq $c; } return 0; } return 0 if scalar @substr; return 1; } Less

public static boolean check(String original, String subString) { if(original == null || subString == null) { return false; } if("".equals(subString)) { return true; } int positionAtOriginal = 0; for(int i = 0; i < subString.length(); i++) { char currentChar = subString.charAt(i); boolean findCurrentChar = false; while(positionAtOriginal < original.length()) { if(original.charAt(positionAtOriginal) == currentChar) { findCurrentChar = true; } positionAtOriginal++; if(findCurrentChar) { break; } } if(!findCurrentChar) { return false; } } return true; } Less

Show more responses
Booking.com

Create monolithic sublists from an array of integers ,in increasing order for example [1,2,4,7,5,6,3,2] will give [[1,2,4,7][5,6],[3],[2]]

5 Answers

my @array = (1,2,4,7,5,6,3,2); my @sub; my $i=0; while ($i<=$#array) { my @set; push(@set,$array[$i]); for(my $j=$i;$j<=$#array;$j++){ if($array[$j]<=$array[$j+1]){ push(@set,$array[$j+1]); } else{ $i= $j+1; last; } } push(@sub,\@set); } Less

$src = [1,2,4,7,5,6,3,2]; foreach (@{$src}) { ++$idx if $dest->[$idx]->[-1] > $_; push @{$dest->[$idx]}, $_; } # result: $dest = [[1,2,4,7][5,6],[3],[2]] Less

use strict; use warnings; use Data::Dumper qw(Dumper); my @main_list = (1, 2, 4, 7, 5, 6, 3, 2); my $prev = $main_list[0]; my @result = (); my $index = 0; foreach my $num (@main_list) { if($prev > $num) { print Dumper \@result; @result = (); $index++; } push @result, $num; $prev = $num; } print Dumper \@result; Less

Show more responses
Booking.com

Convert number above in hexadecimal 312312 Ex. 255 -> FF 254 -> 254 div 16 = 14, int(254/16) = 15. -> F, E Later I found the detailed Division-remainder in source base http://en.wikipedia.org/wiki/Hexadecimal#Division-remainder_in_source_base

3 Answers

#*************************************************************************** # Creator: Zoran Hristov 03-02-2015 # # NAME: # convertDecToHex # # # PARAMETERS: # $decimal - decimal input # # RETURNS: # $rez - hexadecimal representation of the decimal input #*************************************************************************** sub convertDecToHex { my $decimal = $_[0]; my %conversionHash; $conversionHash{0} = '0'; $conversionHash{1} = '1'; $conversionHash{2} = '2'; $conversionHash{3} = '3'; $conversionHash{4} = '4'; $conversionHash{5} = '5'; $conversionHash{6} = '6'; $conversionHash{7} = '7'; $conversionHash{8} = '8'; $conversionHash{9} = '9'; $conversionHash{10} = 'A'; $conversionHash{11} = 'B'; $conversionHash{12} = 'C'; $conversionHash{13} = 'D'; $conversionHash{14} = 'E'; $conversionHash{15} = 'F'; my $decimali = $decimal; my $rez; my $rem = 1; while ( $decimal ne 0 ) { print " curent print reminder decimal \n"; $rem = $decimal % 16; print "curent devision returns $rez\n"; my $ind = $decimal - $rem; if ( $ind = 0 ) { $rez = $conversionHash{$rem} ; } else { print "For curent decimal $decimal we will transalte to $conversionHash{$rem}\n"; $decimal = ( $decimal - $rem ) / 16 ; $rez = $conversionHash{$rem} . $rez; } } print "\n Input decimal value $decimali was translated in hex reresented by: $rez \n"; } # end of sub convertDecToHex Less

my @digits = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F); my $num = 312312; { my $out = ""; use integer; while($num > 16) { $m = $num % 16; $num = $num / 16; $out = $digits[$m] . $out; } print "$num$out\n"; } Less

That's more perlish, universal approach :X perl -e '($n,$b)=@ARGV;@m=(0..9,"A".."F");$o="";while($n>0){$r=$n%$b;$n=int$n/$b;$o="$m[$r]$o"};print $o' 256 16 Less

Booking.com

You have input arrays and one output. The output one contains only the elements that are only in one of the arrays. Fallow u question is what is the complexity of problem and algorithm

3 Answers

I answered as compare every element of the first one if exist in the second one and vice versa Less

sub difference { my ($arr1, $arr2) = @_; %count = (); foreach $element (@{$arr1}, @{$arr2}) { $count->{$element}++; } @difference = (); foreach $key (keys $count) { if ($count->{$key} eq 1) { push (@difference, $key); } } print "Uncommon numbers from both arrays are : @difference", "\n"; } Less

my $arr1 = [1,3,5,6,7,8,9,77]; my $arr2 = [43,6,3,5,234,66,4]; print join ' ', @{difference($arr1, $arr2)}; sub difference{ my ($arr1, $arr2) = @_; my @difference = (); my $count = {}; $count->{$_}++ for (@$arr1, @$arr2); for (keys %$count) { push @difference, $_ if ($count->{$_} == 1) ; } return \@difference; } Less

Viewing 1 - 10 of 100 interview questions

See Interview Questions for Similar Jobs

etl developerdata engineerpython developerphp developersite reliability engineer

Glassdoor has 100 interview questions and reports from Perl developer interviews. Prepare for your interview. Get hired. Love your job.