Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 3 vote

We only need 3 Max positive values and 2 Min neg values. This can be done in 5 passes at most (we can piggy pos and neg and reduce it to 3 passes). Then we only check whether P1P2P3 or P1N1N2 is greater.

- Anonymous September 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about: [-5 -4 -7 -2 -11 -3 0] ?

- Anonymous September 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what about it. The max product is 0

- Anonymous September 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if the max number is zero, the max product is zero

- tylerDurden September 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not really for the cases like this: [-5, -3, 0].

- Anonymous September 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a dupe. Please search the first 2-3 pages of this site and you will find it. Once you do, please click the Report Dup link.

- Anonymous September 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The original solution can be implemented without the need to sort the array

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

We shouldn't be answering in terms of +ve or -ve numbers as there can be cases where all the numbers are either positive or negative. Just find the largest three and the smallest two and then check for which product is bigger. In this case the interviewer may be looking for the most optimum way to find those numbers.

- Algo VisaWe September 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This question definitely needs (2 -ve with 1 +ve) or (3 +ve) numbers.
Lets take the help of data structures to solve this
consider 3 heaps say H1,H2,H3
H1 -> max heap of negative numbers of size 3
H2 -> min heap of negative numbers of size 2
H3 -> max heap of positive numbers of size 3

Iterate through the array of integers and push the integers into their respective heaps (Push 0 in H1 if it comes)
Now H1 and H2 contain our primary results
if (H3 is empty) or (H3 has 2 entries) then take needed values in H1 (This explains all negative values case)
In all the other cases evaluate the required result accordingly

Please note that whatever the value of 'n' is the lookup is finally broken down to 8 numbers at-max

Complexity: It is only one run through the array. so it is O(n)

Note: The heaps solution is proposed as it will be easy to explain and the same can be used for higher problems also (say find 7 integers to find largest product where coding in terms of variables may be difficult)

- SecretAgent September 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your heap solution seems to be good, can you provide a working code?

- Anitha September 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your heap solution seems to be good, can you provide a working code?

- Anitha September 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Your heap solution seems to be good. Can you provide working code?

- Anitha September 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about developing a hash table and find the smallest and largest numbers using hash functions. Using this, we can also reduce the time complexity from O(n) to O(1).

- Vit September 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If it is only [min1,min2 and max3,max2,max1] is what you find, you can do this O(n) in single linear visit of the array. You'll need to spend constant time at each element doing a bunch of comparisons. Why would you want to maintain one or more heaps for this ?

- MarkKnopfler September 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can see many alogs above with +ve and -ve number consideration.. but what if all numbers are -ve?

- Aks September 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If all are -ve, then the answer is max3*max2*max1 anyway.
-40 -30 -20 -10 -3 -2 -1, then of course -6 is your best shot.

- MarkKnopfler September 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Rightly explained Mark!

- VKC October 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

guys.. let it be -ve or +ve numbers..
we need to always find the max1,max2,max3 and min1 and min2
and check whether product of max1,2,3 is greater or min1,min2,max1 is greater..
now regarding DS..
v can keep hash but they have not mentioned anything about range of numbers..
so i feel .. 5 variables or normal linked list will solve the problem..
and no.of comparisons can be easily reduced...
pls correct me if i am wrong..

- mgr October 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can keep 5 variable: max1,max2,max3,min1,min2 and you can fill them by 5 comparisons at each step in a single pass (O(n))

- andy November 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why are we looking for max1,min1,min2 (max1 is max no in +ve nos and min1,min2 are two lowest numbers in -ve numbers) for the case of negative number inclusion.
Is not it should be a,b,c where a = max no in +ve numbers and b,c are two max -ve numbers.

Please clarify.

- ashi September 23, 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