Amazon Interview Question for Software Engineer in Tests






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

Addition:
1 int array is sorted
2 the integer has duplicates

- green September 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the question doesn't look like an "Amazon kinda question".
as the array is sorted all duplicates will be next to each other, so use the standard "Binary Search" itself, what's the issue with that. Efficiency = O(logn)

- red September 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How do you find a pivot (middle int) to do the binary search?

- green September 09, 2009 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

use sizeof() function
middlepoint=(sizeof(array)/sizeof(int))/2

- satheesh January 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

As you are required to find out only one match, the best way to do it is to check the first element(2^0), 2^1, 2^2....2^n,until you get one greater than desired. (I assume it is in increasing order)

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

if end of array comes,then that value is garbage so this method fails

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

Anonymous, that is a great idea. Or instead of exponential increase, we may use Fibonacci sequence instead.

- hunter September 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume the element to be found is 'ElementToBeFound' and Array be 'A'
Start with index = 1, lastIndex = 0.

While( A[index] < ElementToBeFound )
{
lastIndex = index
index = index * 2
}
We have found the range where element could be found using reverse binary search
now use normal binary search to find element between A[lastIndex] and A[index]

- Avinash September 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if we need 6th element then ur soln accessing a[8] which is garbage so...

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

Anonymous and Avinash, the time complexity of such an algorithm is
O(log n + log n/2 + ...)
=O(log^2 n)

- Hero September 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Hero. Why would the complexity be 0(log^2 n) for Avinash's approach ? Wouldn't the loop be The loop would be O(n) in worst case & then the binary search be 0(log(index-lastIndex)) ?

- amit October 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Avinash:
I agree that we need to have some way to make a good jump once we know the current element is less/equal/greater than the element we're looking for. But I'm not able to figure out what made you guys to think of the jump by doubling the index because the Q asks for Binary search?
As Hero already mentioned the complexity says the algo is pretty expensive, though the solution does work. Any other way of doing the same with more efficient solution?

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

jump further and further then retract

- Anonymous October 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suppose we have to find the element (integer), say i from the infinite array arr[].
In that case this is the pseudo-code of binary search that might work:
1. Jump to the ith location in the array
2. Let j = i
2. if (arr[j] > i) {
{low=j+1;}
{high=j+i;}
{j = j+i/2;}
} else {
{low=j-i;}
{high=j-1;}
{j = (j-i)/2;}
}
}

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

Can we use some random numbers for getting the pivot element ?

- neo March 30, 2010 | 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