Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

problems : a + b + c = 0 -> a + b = -c
as we known : in sorted array, find a, b that a + b = M (M is const value ), the algorithm is very simple and its time complexity is o(n)
so, my algorithm is :
step1: sort the array . o(nlogn)
step2: for each value c in the array :
find a + b = -c
at last, the time complexity is o(nlogn + n * n) = o(n^2)

- Anonymous January 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This seems to be O(n^3) algorithm.
For a given c, finding (a,b) pair such that a+b = -c would take n^2.
and in worst case, you need to try all possible c, i.e. and so
n* O(n^2) = O(n^3)

- Anurag Singh January 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No finding a pair that sums to a specific value can be done in O(n) given the array is already sorted. Copy paste from another site.

If a+b = K, then return (a,b).
If a+b < K, then move a to the right one position.
If a+b > K, then move b to the left one position.

- Rayden January 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Above may not work in worst case..
e.g. -6, 0 , 1, 2, 4
answer is: -6, 2, 4
To start with -6, we need to find a pair with sum 6
and probably search pair pattern would be:
(0,1), (0,2), (0,4) -- not found
(1,2), (1,4) -- not found
(2,4) -- found
And this search doesn't seem to be O(n). In worst case it will be:
(n-1) + (n-2) + (n-3) + .... + 1 = O(n^2) times

- Anurag Singh January 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

At the beginning a is at the start of array, b is at the end of the array. Now reexecute the code and see for yourself. Again the array MUST be sorted.

- Rayden January 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution would be in O(n^2) only if we use a hash table.

To pick 2 numbers a,b from an array takes O(n^2) in itself.
we can search for c such that c = -(a+b). If we would have hashed all the values then we can lookup if the value -(a+b) exists in the hashtable ensuring overall running time of O(N^2).
For this approach, the array need not be sorted.

- Harish January 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

{ -6, -4, 0, 12, 13 } will fail for this integer list.

12+ -6 = 6
when i do a search for -6 in the hash table it would print -6, 12 for sum 6 which is false.

- helloworld January 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good observation, we need to keep track of the 12 and -6 position so that the -6 will not hit those two positions.

- yorkzhangcs January 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There can be one case also
a= -(b+c)
there can be one +ve element say 10 and there can be two negative no -6 and -4 so it will fail for this case as well

- khush January 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

stackoverflow.com/questions/2070359/finding-three-elements-in-an-array-whose-sum-is-closest-to-an-given-number

solution

- blackice February 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 vote

The above solution will work just fine. There is another approach as well.

You can also traverse the array and put everything into hash table. Once this is done try all the possible pair sums and check if negative of the sum exists in the hash table. This will be O(n^2) as well.

- Rayden January 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This would be more efficient and it will show your understanding of data structures.

- Shreyas January 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

1. Form a hashtable<sum, <pair>> : store all possible sum in key and the forming values in a pair. : O(n^2)
2. for each value 'k' in array, see if its negative exists in any key(sum). If yes , return the 'k',pair corresponding to sum. : O(n)
total complexity: O(n^2)

- Anonymous January 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Sort the given array (say sorted array is a). O(n log n)
2. Create a n x n matrix (say sum) where sum[i][j] = a[i] + a[j]
This matrix is sum of all possible pairs in given array where sum value is sorted
left to right and top to bottom. O(n^2)
Here diagonal elements can be ignored and also upper and lower triangular are same,
We need only upper or lower, so we may calculate only upper triangular portion of the
matrix.
3. Now for each element c in the array, search for -c in the sum array as below:
a) start from sum[0][0]
b) if current element in sum is equal to -c, you got the answer (a[i] + a[j] + c = 0)
c) if current matrix element is less than -c, go right in the same row
d) if current matrix element is more than -c OR you reached at the end of the row
,go down in the same column
e) If you reached at the last element of matrix (bottom right) and -c not yet found,
-c is not in the matrix
Step 3 will take at most 2n search for any -c (if your search happens to be
right, down, right, down, right, ..........), so total O(n^2)
Overall complexity: O(n log n) + O(n^2) + O(n^2) = O(n^2)

- Anurag Singh January 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why u need extra space it can be done without taking the extra space also in O(n^2).
its sure that either 2 positive numbers in 3 or two negative nos are there use this fact
1. sort the array.
2.check that a[0]>=0 then return not possible at all.
3. find the position "pos" such that from here positive numbers start in O(n) just traverse the array.
4. now --
a. take element from i=0 to pos-1 and search in positive part of the array for two numbers such that a+b=-1*c this can be done in O(n2). if found just print those numbers and return.
b.similar as step 4a now just take i=pos to n-1 and search two nuymbers in the negative part such that a+b=-1*c this again in O(n2) if find print the numbers and return
5 return
hope it works in all the cases

- geeks January 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I believe step 4(a) will take (n^2) for each element search and searching is needed for each array element in worst case so it becomes O(n^3). My others two comments I gave already where I said the same.
If a pair of number (a,b) can be found such that a+b = -c for a given c in O(n) time, we are good.
But I don't see how can we find a pair in O(n).
If you don't mind, could you please explain if you have any thought on that (You may want to look at my other two comments 1st on top)

- Anurag Singh January 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

explanation of findinf two pairs such that a+b=c
suppose arr[1...n] is array which is sorted as first step is sorting ok
now take left=1 and right =n and keep looping till left <right
and check in loop:
while(left<right)
{
if(a[left]+a[right]==key)
{
//we got it
break;
}
else if(a[left]+a[right]<key)
{
left++;
}
else
{
right--;
}
}
i think now u got it

- geeks January 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Wat about this guys .. This is my solution when the array is sorted . First of all I am hashing all the values into an hash array and then I am traversing the array
int value=0;
for(i=0;i<a.length();i++)
{
if(hash(value-(a[i]+a[i+1])))
break;-> We have found the value
else
continue;
}

When the array is not sorted the program would be, same as the first I am hashing the values into hash array
int value=0,number=0;
for(i=0;i<a.length();i++)
{
number=value-a[i];
for(j=0;j<a.length();j++)
if(j==i)
continue
else
{
if(hash(number-a[j]))
break;-> we have found the series
}
}
This is O(n^2) solution .. Let me know your comments guys

- Anonymous January 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Make HashTable from All Element
Now
for (i=0;i<n;i++){
for(j=0;j<n;j++){
if(i!=j){
int sum=a[i]+a[j];
//find (-)sum in hashtable
//if it is there then we have 3 Element whose some is zero a[i],a[j],and -sum
}
}
}
this will take O(n) +O(n^2)
which is O(n^2)

- khush January 24, 2012 | 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