Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Here is my answer to this question. The question is not clear. Are we just check one element in the Matrix? Do we need to check multiple ones?

basicalgos.blogspot.com/2012/03/given-matrix-of-0s-nd-1s.html

- Andy March 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming the matrix is int[m,n] a, you just need to check the elements in submatrix [1,1] to [m-2,n-2]. If a[i,j] is 0, then you set the edge of its col and row be 0. In this case a[0,j],a[m-1,j],a[i,0],a[i,n-1] be zero. After checking all elements then check the edge of the matrix, if a[i,0] and a[i,n-1] are both 0 then set all elements in row i be 0. Similarly if a[0,j] and a[m-1,j] are 0 then set all element in col j be 0.
Before all this. You need 4 variables to record if elements in specific edge should be set to 0.
Ex:
1011
1111
1101
1111

First you need 4 variables, bool left=false,right=false,top=true (because a[0,1] is 0), bottom=false. Also set a[3,1] be 0 because a[0,1] is 0:

1011
1111
1101
1011

Then check sub-matrix from a[1,1] to a[3,3], finding a[2,2] is 0. So set a[0,2],a[3,2],a[2,0],a[3,0] be 0:

1001
1111
0100
1001

Now check the edges again and reset all cols and rows with edge be 0:

1001
1001
0000
1001

Finally check 4 variables again and reset edges:

0000
1001
0000
1001

Time: O(MN)
Space:O(1)

- gztyjz March 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
int main()
{
int a=3;
int b=3;
int matrix[3][3]={ {1,0,1},{1,0,1},{1,1,1}};
int row[a];
int col[b];
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++)
{ if( matrix[i][j]==0)
{
row[i]=1;
col[j]=1;
}
}
}
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++)
{if((row[i]==1) || (col[j]==1))
{
matrix[i][j]=0;
}
}
}
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++)
{ cout<<matrix[i][j];
cout<<"\t";
}
cout<<"\n";
}
getch();
return 0;
}

- saurabh(vit) March 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Extra space.Really? Even a koala can write that code.

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

extra space is the best way to solve this.
DO you have a solution which does not use extra space...pls paste it...dont give troll comments which cant be verified

- Anonymous April 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

m = row[0].length;
n = row.length;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(a[i][j] == 0)
fill(a,i,j)
replaceAllMinusOneWithZero(a);
return a

fill(a,i,j)
for(int x =0;x<a[0].length;x++)
if(a[x][j] == 1)
a[x][j] = -1;
for(int y =0;y<a.length;y++)
if(a[i][y] == 1)
a[i][y] = -1;

- dell March 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice. But, what if it is a bool matrix only?

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

O(1) space and O(n*m) time:
1) travel normal row and column way in a matrix using two loops.. let it is i,j.. initially flag=0
2)if at any point v encounter 0 thn make flag=1 and make all elements upward of this column as 0.
3) if step 2 is not satisfied then check whether element just above it is 0 or not.. if it is thn make current element as 0 but do no change flag.. (this step is to make all elements down to original 0 element as 0)
4) now travel ahead in the same row to check for further 0s. if again zero is found then make all elements upward in the same row as 0..
5) when u reach end of row, then check whether flag is 1 or not.. if it is 1 then make complete row as 0..
6) go to next row and make flag=0 and repeat all above processes..

This way v r just making all elements above to 0 as zero and changing flag as 1 .. if current is not zero thn check upward element and if it zero thn make current element 0 but do no change flag..

Hope this HELPS...

- SHAN March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

adding further:
Remember: during making of elements upward to orginal 0 as 0, move only upto tht level upward till u do no encounter 0.. once u encounter 0, stop there.. becoz elements upward of tht 0 will already b 0...

- SHAN March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

in STEP 4: i hv written at d end of line " make all elements upward in the same row as 0 ".. it shud b "make all elements upward in the same COLUMN as 0"

- SHAN March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wont work.
If there is one 0 in the top row...u change all the row elements to 0.
when i am in the next line and if i look up..i will find only zeros....
which means everything below will turn into 0.

- mulki April 03, 2012 | 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