Booking.com interview question

zoek voor een reeks integers de eerste reeks waar in de som gelijk is aan de gegeven waarde en leg de moeilijkheid uit.

Interview Answers

Anonymous

6 May 2013

public static int findSubArrayThatSumsTo(int[] A, int S) { int sum = 0; int j = 0; for (int i = 0; i S) { sum-=A[j]; j++; } if (sum == S) { System.out.format("Sum (%d) found between indices %d and %d\n", S, j, i); break; } } return j; // return start-index }

9

Anonymous

6 May 2013

Complexity of above is O(n). From the look of it (given two loops), it might seem that complexity is higher. But actually every element is accessed utmost twice. So, O(2n) = O(n).

4

Anonymous

29 Apr 2014

The answers with keeping the running sum whilst iterating over the array, and advancing the beginning of the sequence pointer if the running sum is greater than the desired sum, only work for sequences of all positive number, right? For arrays that allow negative numbers, we need something 'smarter' like e.g. calculating the array of partial sum and finding the pair with the desired difference - O(n log n). Counter example - look for 5 in the sequence of {1, 7, -3}. The above solutions would find none, whereas the right answer is {1, 7, -3}

2

Anonymous

25 July 2014

use strict; use warnings; my $str = "0F3A45JA"; my @input = split (//, $str); my $bin; my @out; foreach my $in (@input) { next, unless ($in =~ m/[0-9A-Fa-f]/); $bin = sprintf( "%b", hex( $in) ); push (@out, $bin .= 0 x (4 - length($bin))); } foreach (@out) { print "$_\n"; }

1

Anonymous

12 Aug 2014

#!/usr/bin/env perl use Modern::Perl; use Data::Dumper; my $array = [1,3,4,2,6,7,4]; sub find_seq_with_sum { my ($n, $list) = @_; my ($result, $s) = ([], 0); for (@$list) { while ($s > $n) { $s -= shift($result); } last if $s == $n; # $s < $n push($result, $_); $s += $_; } return $result; } say Dumper(find_seq_with_sum(13, $array));

Anonymous

8 Sept 2014

sub s_sum { my ( $arr, $total ) = @_; for ( my $i = 0; $i [$j]; push @find, $arr->[$j]; return \@find if $sum == $total; } } }

Anonymous

8 Sept 2014

sub s_sum { my ( $arr, $total ) = @_; for ( my $i = 0; $i [$j]; return [ ( @$arr )[$i..$j] ] if $sum == $total; } } }

Anonymous

8 Sept 2014

sub s_sum_1 { my ( $arr, $total ) = @_; for ( my $i = 0; $i [$j]; return [ ( @$arr )[$i..$j] ] if $sum == $total; } } }

Anonymous

20 Apr 2013

Could you tell us what other questions they asked you?

Anonymous

15 June 2013

Python answer: def sumSeq(seq,target): #return list of couples of indices that can be used as slices of seq that sum to t for a in range(0,len(seq)): for b in range(a+1,len(seq)): t=sum(seq[a:b]) if t>target: break if t==target: return (a,b) return None ,,,and I will spare you the extensive tests.

Anonymous

15 Apr 2013

Integer[] input = new Integer[] {3, 3, 2, 1, 2, 45, 32, 1, 23, 4}; // same array to test Set uniques = new LinkedHashSet(); for(int i=0; i