Amazon Interview Question for Software Engineer / Developers






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

Suppose C is the smallest array. Sort A and B, and for each element x in C, do the usual algorithm to find x in the implicit 2D array [Sij] where Sij = Ai + Bj.

No one knows how to do (much) better without some restrictions on the input.

- Anonymous March 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

well he said all the 3 lists are of same length n..

- kanurukh March 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's not necessary for correctness.

- Anonymous March 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question says that the sum of the chosen numbers from each list should be zero. How does your algorithm ensure that ?

- abhimanipal April 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess solution might come from Dynammic programming.That might reduce the o(n^3) complexity of brute force algo into somethings less.

- Anonymous March 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A O(n^2) complexity could be:

1. Convert array A to Hash Table
2. For any pair of element c in C and b in B, find if -(b+c) is in the hash table.

Not very if lower compexity solution could be found or not

- Anonymous March 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort A,B,C, then modifying merge sorted to search for A_i+B_j+C_k. Time O(nlogn) , n is the length of A or B or C

- Anonymous March 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How? (If this works, you should publish it.)

- Anonymous March 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL !!

- sachin323 March 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A solution with O(n) complexity.

Create a hash table, that contain all the possible sum combination of any of the 2 list.

A X B

A[1] + B[1....n]
A[2] + B[1....n]
...
...
A[n] + B[1....n]


Its a one time effort with O(n*n).

And it provide lateral access & solution with O(n).

Check if -(C[index]) exists in the hash table then return true.

- Ashutosh March 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, it has Theta(n^2) complexity.

- Anonymous March 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can think of only hash as a sol can anybody give better solution

- destiny March 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

When you mention a hash table mention the hash key and hash function because hash table is nothing without it.And only with that detail we can determine the worst case complexity of the solution.

for each of n*n element combination from A&B build a binary search tree. Building a tree takes O(N) where N = n*n.

then for each of n elements from C, Do a binary search on the tree which is expected to be O(nLgn) thus the Overall complexity of the solution remains O(n^2)

- Harish March 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Right! there could not be a better answer!! (at least as of now :))

- clrs March 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Building a search tree is at least O(NlogN). So this fails as N = n^2 and this goes over O(n^2).

This is a silly solution. Even more silly for you to think this is the best one...

- Anonymous March 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

First sort all of them (O(nlogn) complexity)
A -> ascending
B & C -> descending

Now to make understanding simpler,lets take an example

A=> -4 -1 3 6 7 8 i

B=> 9 7 3 2 1 -5 -6 j

C=> 12 5 2 1 0 -1 -5 k

LOOP { // exit if i+j+k == 3n
1.check if A[i] + B[j] + C[k] ==0,if yes return true;

2. if not then if A[i] + B[j] + C[k] <0
i = i + 1
3. else {
if(A[i] + B[j+1] + C[k] == 0) return true;
if(A[i] + B[j] + C[k+1] == 0) return true;
j = j+1;
k = k+1;
}
END LOOP

The idea is to
=> if sum < 0 ,then value at A should be increased to get zero so i is incremented
=> if sum > 0 ,then B and C should be decreased to get zero so j and k is incremented.

Complexity = sorting + interation
= o(nlogn) + O(3n)
= O(nlogn)

pls note :- Just for sake of understanding,i have taken i,j,k;They can be implemented using ->next in a linked list.

Guys,ur comments are welcome!

- rajan March 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This doesn't work, since the "right" elements in B and C may be further than one apart.

- Anonymous March 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It doesnt work in the following scenario (Ideally it should have returned true as A[0] + B[3] + C[1] = 0)

A {0, 1, 2, 4, 5}
B { 4,3,1,-1, -4}
C { 2,1,0,-2,-4}

First loop:
A[0]+B[0]+C[0] = 6 >0
&& A[0]+B[1]+C[0] != 0
&& A[0]+B[0]+C[1] != 0
j=1
k=1

Second Loop:
A[0]+B[1]+C[1] = 4 >0
&& A[0]+B[2]+C[1] != 0
&& A[0]+B[1]+C[2] != 0
j=2
k=2


Third Loop:
A[0]+B[2]+C[2] = 1 >0
&& A[0]+B[3]+C[2] != 0
&& A[0]+B[2]+C[3] != 0
j=3
k=3

Fourth loop:
A[0]+B[3]+C[3] = -3 <0
i=1

Fifth loop:
A[1]+B[3]+C[3] = -2 <0
i=2

sixth loop:
A[2]+B[3]+C[3] = -1 <0
i=3

seventh loop:
A[3]+B[3]+C[3] = 1 >0
&& A[3]+B[4]+C[3] != 0
&& A[3]+B[3]+C[4] != 0
j=4
k=4

eighth loop:
A[3]+B[4]+C[4] = -4 <0
i=4

ninth loop:
A[4]+B[4]+C[4] = -3 <0
i=5 <<Loop Ends>>

- Anonymous March 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

3SUM-Hard problem. Look it up and stop wasting everyone's time with sub-quadratic solutions.

- Anonymous March 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My algorithm would do...
minA = lowest element in A (On).
minB = lowest element in B (On)
minC = lowest element in C (On).
if minA + minB + minC > 0 => return false
else return true.
This would be On overall.

I think this would solve the problem. Or am i crazy?

Thanks.

- ManoX March 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are crazy.

- Anonymous March 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ManoX : WTF ????

- Anonymous March 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

HHHUUUAAAAAAAAAHAHHAHAAHHAHHAAH.....HHUUAAHHAHHAHAHHAHAHAHHAAAA..awesome answer. i dont need coffee now to carry on for more 2-3 hrs.

- Anonymous May 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ooops yes i think i am crazy too. Sorry, i misunderstood the problem. I thought it could be <= 0. That would be too easy :)

- ManoX March 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can anybody give foolproof ans

- Anonymous April 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you guys are so funny! I have some rough idea of O(n^2) complexity.
first construct hashtable for numbers in B and C, setup flag indicating which list each number belongs. like this
Hashtable:
key value
number B or C or BC

then iterate each number in A, suppose the value of one number is m
then you need to find the sum of numbers from B and C which sums to -m
do linear search in hashtable. it can be done in O(n) since number value
is the key of hashtable.

- Jovi June 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you guys are so funny! I have some rough idea of O(n^2) complexity.
first construct hashtable for numbers in B and C, setup flag indicating which list each number belongs. like this
Hashtable:
key value
number B or C or BC

then iterate each number in A, suppose the value of one number is m
then you need to find the sum of numbers from B and C which sums to -m
do linear search in hashtable. it can be done in O(n) since number value
is the key of hashtable.

- Jovi June 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

U can do it in O(n^2 log n)
Sort C. Now for each pair of (a,b) binary search to see if the required c exist.

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

{
Sort B, C

for all A[i]
{
j=0;
k=n-1;
while(j<k)
{
if B[j] + C[k] < - A[i]
j++
else if B[j] + C[k] > - A[i]
k--;
else return true
}

}

return false
}

- nik July 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This becomes more interesting if -ve numbs are allowed in the input.
Use 3 min-heaps for each of the lists. And check whether the topmost elements sums up to 0.
P.S: this works only if the arrays are sorted, if not you should heapify at the start O(nlogn).

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

@arvind646 : ya ur sollution is right really fine using the heap and maiontaining the sum as variable always 3 member be there at the heap and its so easy time coplexity over all that is taking sorting as werll be nlgn a really gud slution means we are taking sme extra space that is constant too means heap will always be of size 3 :)

- geeks August 02, 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