Interview Question

Perl Developer Interview

-

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]]

AnswerAdd Tags

Interview Answers

5 Answers

0

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); }

Anonymous on

0

$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]]

EvilCartman on

0

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;

Anonymous on

0

public static List> GetInc(int[] nums) { List> result = new List>(); List temp = new List(); int last = int.MinValue; for (int i = 0; i (); } temp.Add(nums[i]); last = nums[i]; } result.Add(temp); return result; }

Anonymous on

0

Code in JAVA import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Question: 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]]" * Created by hitesh.sethiya on 30/07/16. */ public class MonolithicSublists { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); List list = new ArrayList(n); for(int i = 0; i sublists = new ArrayList(); for(int i = 0; i < list.size(); ++i) { while(i+1 < list.size() && list.get(i) <= list.get(i+1)) { sublists.add(list.get(i)); ++i; } sublists.add(list.get(i)); sublists.add(Integer.MAX_VALUE); } for(int i = 0; i < sublists.size(); ++i) { if(sublists.get(i) == Integer.MAX_VALUE) { System.out.println(","); } else { System.out.print(sublists.get(i)); } } } }

Hitesh Sethiya on

Add Answers or Comments

To comment on this, Sign In or Sign Up.