Amazon Interview Question for Developer Program Engineers






Comment hidden because of low score. Click to expand.
0
of 0 vote

try to do it in O(n)

- manni October 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Lets take input array as a[]={-3,2,4,-6,-8,10,11}

Make an array b [] such that b[i]=b[i-1]+a[i];

So b[]={-3,-1,3,-3,-11,-1,10}

Now all those pairs at indexes (i,j)in array b[] having equal numbers are the indexes of subarrays from (i+1,j) having sum as zero.

Finding all pairs in O(n) can be done by using map.

- nittu October 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

b[0]=0 otherwise it will fail if the i/p is "4,-6,-8,10"

- chukka.swathi December 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Same idea explained:
b[i] = b[i-1] + a[i] - this means that b[i] = sum(a[0],..., a[i]);
we have to find some (i, j) so: a[i] + a[i+1] + ... + a[j] = 0. But a[i] + a[i+1] + ... + a[j] = b[j] - b[i-1].
when creating the map using b values, the key in map is the value from b and the value in map is the index of from b. if a new key exists, this means we found a solution.
in the ex:
-3 => 0
-1 => 1
3 => 2
-3 => 3 (-3 exits, so, the solution is from first index + 1, 1, to current index, 3)=2, 4 , -6
-11 => 4
-1 => 5 (-1, exists, so, the solution is from 2 (1+1), to 5 = 4, -6, -8, 10
10 => 6
We found both solutions (or none, if there are no duplicates in b).

- S October 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this doesn't give all pairs , what about the subset { -3,2,4,-6,-8,11} they sum to 0 too

- smk November 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the one you give is not a subsequence of the input sequence.

- Kirk November 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

put a if condition for "0" also and compute the sequence..

- Newbie January 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how r u checkng dat -3 alrdy exists?

- @above October 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

using a map.

- S October 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's a trivial solution with bad time complexity :( . But still a solution :) !!
This is the first solution that strikes me.

We can sort the array in O(nlogn) using quicksort.
Then, we will generate all possible sum of the negative number and store the corresponding elements using Map.
So two map are created with all possible sum of positive numbers and negative numbers respectively. Sort these map.
Check it in these two map, two numbers are equal(Ignore the sign, obviously the sum of negative is always negative). All elements of negative and positive numbers stored corresponding to those two location in two map(+ve number and -ve number) will be the answer.

- Aditya November 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

NA

- weijiang2009 February 06, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More