Amazon Interview Question for Software Engineer in Tests






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

Do in-place sorting...and follow this algorithm
i =0;
n is length of array
while(i<n){
if( (a[i]*a[n])==number){
return i and n as pair;
}else{
if( (a[i]*a[n])< number){
++i;
}else{
--n;
}
}
}

return no such pair;


Complexity O(nlogn)

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

In worst case, complexity (of quick sort which sorts the array in place) is O(n^2)

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

Heap sort will do the trick, its a in place algorithm with worst case time complexity being O(n log n).

- Triton August 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hash all the elements in the integer array. Now search Product/number in the hash, this is O(1) operation, So on the whole it takes O(N).

- Messi April 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The condition is not using any extra space. I guess using hash will consume memory

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

1. Sort the array.
2. for each element find (product/element) is the array by using binary search.
3. If u find such element the your pair would be element & (product/element).

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

Binary search won't work here. Binary search aliminate half of element in each pass. It could be possible that one of element is from that list.

i guess first solution above will work

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

@Anonymous-April12
u didn't take into account that integers could be negative. so the sorting and search should be based on absolute vales . Then the code works fine..

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

take the factors of product .. let say the product is 12 .. its factors are 1 2 3 4 6 12
So your first element in the pairs should be one among them ,
lets take 1 first and see if there is any 12 in the array , if yes then that is a pair (1,12) , if not there is no such pair
lets take 2 and see if there is any 6 in the array , if yes then ( 2,6) else none
take 3 and see if there is any 4 in the arry , if yes then ( 3,4) else none

similarly we can continue for other factors of the product .. here there is no extra memory needed

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

But here you will store the factors in extra array...
Doesn't it violates the condition of not using extra space?

Otherwise this is good algorithm. and complexity will be (n*f) where n is number of elements in array and f is number of factors of product.

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

product does not mean its between two numbers, it can be more than 2. for example 12=2*2*3

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

If no extra space, then its a O(N^2) solution.

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

Something like this should work:

Sort the array using quicksort.
Let the give number be n.
Have one pointer at the beginning(i) and one at the end(j).
while(i<j){
If a[i]*a[j]=n //result is(a[i],a[j])
else if a[i]*a[j]>n
j--
else
i++; }

- Agent May 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry..did not notice. This solution has already been provided.

- Agent May 04, 2010 | Flag


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