Amazon Interview Question for Software Engineer / Developers






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

I found the following solution from some other site:
Let X be the bottom left element of the matrix and we need to find the position of an element K.
Now if K = X, we can return the position of X.
Else if K < X, then we can possibly eliminate that row as all the elements in that row are greater than or equal to X.
Else if X < K, then we can eliminate that column as all the elements in that column are lesser than X.
Now in each step, we eliminate either a row or coll which finally leads to that element.

- madankumarrajan April 13, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 vote

@ madankumarrajan
i think this is nice soln.

- Ishwar4frendz August 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In my opinion the solution should go like this:

Search rowise first:
start looking for element in Row=0 and as soon as you find two columns, one which is less than your SEARCH number and one which has higher than your SEARCH number. So we can look for our number in these 3 columns, which is worst case would give us a running time of 3N i.e. O(N).

Please correct me if I am wrong or if you have any other interesting solution.

- Daniel Johnson February 17, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

suppose the matrix is:

1 2 4 300
3 5 6 400
10 11 12 500
100 101 102 600

search for 11

according to your algorithm, first find two columns, 4<11<300
then search the 3rd and 4th column for 11, apprently u find null

- colinlee February 18, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Vairaghi,

It looks like you had an onsite Amazon Interview. Can you share your experience. I also have one in 2 weeks time in Seattle. Any helpful tips will be greatly appreciated.

Thanks in advance.

- Daniel Johnson February 17, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we should search the diagonal of the matrix which is sorted, then search the row and column.

Since every row/col/diagonal is sorted, we can even use binary search to get a O(logn)

- colinlee February 18, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 0 votes

This is a tricky one. i myself spent
ample time looking for linear time.
the answer is pretty simple. start
with top right element and do this :

1 compare the element(e) with query element(q)
2 if they are equal bingo, return (FOUND)
3 else
if e > q
move pointer to left if u can, else return
as UNFOUND
else
move pointer to down if u can, else return
as UNFOUND

- now repeat from step1

in step 3, you always ignore one row or column,
so it runs in O(row_count + col_count). it
converges as you will eventually an edge and cannot move further. remember we will only move
down or left.

- moulaali February 25, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome solution

- Daniel Johnson February 28, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome solution moulaali ... only one i have seen that is simple and works good

- selekt November 11, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 11
10 12

i am finding 11 ...
so e = 1 and q = 11...
according to the algorithm i will never find 11 ... would I?

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

We have to start from the right top side

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

tricky question. don't know why, but my first instinct was to think that the last element in the first row will be less than the first element in the second row!

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

My approach would be to divide the matrix.

problem:

10 11 12 13

14 15 16 17

18 19 20 21

22 23 24 25


Find 19....
we know that the element at the right most bottom is the largest element. compare element_query() with this element. if geater then no found else

compare with the next diagonal element 20. 19 < 20 . we can assume that element falls on the left of the diagonal. fine... compare the query element with the next diag element. 19>15 so element can be on the right on on the next row. element cannot be on the right of the diag as we found out. hence search the row corresponding to 20 to find 19. If present return true else element not found.

- nayak_khalnayak March 25, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

search(int row, int column, int value)
{
if( 0<= row < n && 0<= column<n)
{
if(array[row][column] > value)
search(row, column-1, value);
else if(array[row][column] < value)
search(row-1, column, value);
else
return true;
}
else
return false;


the very first time call is search(n,0,value)

- Anonymous August 16, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In O(N) loop through all rows to find the row in which the
element can be, then logN to search through that. N+logN still a linear solution.

- mp April 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actually we can do it in 2*logn time.Perform binary search row wise :
T(n,n) = 1 + T(n,n/2); logn
Then finally we are left with one column to search for.....
Now perform binary search on this column...logn

Total : 2*logn

- Stranger April 09, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

2 Functions I have:
int doBinSearchOnRow(int row, int tillCol, int num); //returns index of match or the index of the closest number < num.
int doBinSearchONCol(int col, int tillRow, int num);

Solution:
int c = doBinSearchOnRow(1,n,num);
if a[1,c]== num return true;;
int r = doBinSearchONCol(1,n,num);
if a[r,1] == num return true;
int i = doBinSearchOnCol(c,r);
if(a[i,c] == num) return true;
i = doBinSearchOnRow(r,c);
if(a[r,i] == num) return true;
return false;

- criminal April 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i cogitate moulali's sol......

1> start from left bottom most cell.
2> if query is larger move right .
3> if query is smaller move up.
4> if u can't move right or up n haven't found the query yet....query doesn't exist.

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

the same question has been asked in this forum HELL of times. guys please do not repost the same question, make sure you search the question out here before you do any posting.

- Anonymous September 25, 2009 | 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