Amazon Interview Question for Software Engineer / Developers






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

Start scanning each row from top to bottom. Stop when find a '1'
Scan each row from bottom to top. Stop when u find a '1'
Scan each column from left to right and then right to left. stop when u find a '1'
This will give u the largest rectangular matrix.

- asdf October 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi
please explain me on some example, or provide me some sample pseudo code to understand clearly

- rider October 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the idea is to cover maximum number of 1's with a rectangle.
so, from all directions u find the first row/column that has a '1'. this rectangle will definitely be the one u need.
finding a square could be tricky, I dont think i have a solution for that

- asdf October 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

When you said each column, then it should be each column from right. If you will do from left then that will give maximum matrix.

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

This problem can make me think of multiple option strategy to relate with known problems :
Directed Graph
Dynamic programming
Determinant Strategy
- These are the one i can think on top of my head.

Best suited seems to be a Dynamic programming.

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

It's almost the same as another interview question from MS that I've heared about.

Some of you may have misunderstand the problem, it asks you to find the largest square matrix, whose elements are all '1'.

The trick is to use DP, and best algorithm can do it in O(M*N).

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

geniusxsy: can you please explain the way to go ?

- restlesstoday October 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry I said DP, I meant divide-N-conquer

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

for (a), halve the matrix into two pieces, so the largest square can be:
in the left region --- recursive call on the left region
in the right region --- recursive call on the right region
cross the mid separation line --- O(N*M)
So, finally:
T(N,M) = 2 * T(N/2,M) + O(N*M)
--> T(N,M) = O(N*M) assuming M is Omega(logN)

Only tricky part is, how to implement this in the O(N*M): find the largest square that crosses the mid line.

Well, it's not that difficult anyway, since O(N*M) is a pretty loose requirement.

- geniusxsy October 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain the method to find the largest rectangle across the mid line ?

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

for(b), it's a more difficult to find the largest rectangular that crosses a separation line, since rectangular has more flexibility than square. But still, you can do it in O(N*M), with more complicated code. Sorry I can't find an easy way to illustrate that without a draft paper.

- geniusxsy October 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

its simple....

for each row in the separation line that has a one.....(sepearation line = floor(M/2))
count the no of adjacent ones in the row which includes that one....lets say X ones in the ith row...

at the end find l such that l is the maximum number of consecutive rows that all have the same no of ones in them.....

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

1. find the largest square matrix with 1's

The largest matirx will be the original matrix itself :)

- Erik October 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ Erik:

The original matrix is MxN not MxM

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

{
"A" being the original matrix
SUB[i][j] = {
A[i][j]
if (A[i][j] = 0 || SUB[i-1][j] = 0 || SUB[i][j-1] || SUB[i-1][j-1])
min(SUB[i-1][j], SUB[i][j-1], SUB[i-1][j-1]) + 1
if otherwise
}
}
Matrix SUB will give you the modified matrix with the lowest right corner of each submatrix having the count.

- TP October 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct approach, wrong code

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

If Matrix(i.j) != 0 then OPT(i,j) =OPT(i, j-1) + OPT(i-1,j) -OPT(i-1, j-1) else
OPT(i,j) = 0;

So we can get it in O(m*n)

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

int Source[M][N]; // initial matrix
int Result[M][N]; // matrix with the result

for(int i = 0; i < M; i++) Result[i][0] = Source[i][0];
for(int j = 0; j < N; j++) Result[0][j] = Source[0][j];
for(int i = 1; i < M; i++)
{
	for(int j = 1; j < N; j++)
	{
		if(Source[i][j] == 0)
		{
			Result[i][j] = Source[i][j];
		}
		else
		{
			Result[i][j] = min(Result[i - 1][j], Result[i][j - 1], Result[i - 1][j - 1]) + 1;
		}
	}
}

Now let's Result[i_max][j_max] is the largest value in the Result matrix. Then (i_max, j_max) - indexes of the bottom right corner of the answer submatrix, and Result[i_max][j_max] is the size (dimention) of the answer submatrix.
I.e., the answer of the question is a submatrix of the Source matrix Source[i_max - Result[i_max][j_max] + 1 .. i_max][j_max - Result[i_max][j_max] + 1 .. j_max].

- Aleksey.M January 21, 2013 | 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