Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

There are n*n elements, so you can't possible have a O(n) solution, but O(n*n).

// prints all valid positions in O(n*n) with extra O(n) space.
// If the interviewer just wants a boolean Possible/Impossible, just return true/false
void FindOut(int matrix[1000][1000], int n) {
    int row_zeros[n], col_ones[n], i, j;
    for (i = 0 ; i < n ; i++) {
        row_zeros[i] = col_ones[i] = 0;
        for (j = 0 ; j < n ; j++) {
             row_zeros[i] += matrix[i][j] == 0;
             col_ones[i] += matrix[j][i] == 1;
        }
    }
    for (i = 0 ; i < n ; i++)
        for (j = 0; j < n ; j++)
            if (matrix[i][j] == 1) {
                if (row_zeros[i] == n-1 && col_ones[j] == n)
                     printf("position (%d, %d)\n", i, j);
            } else {
                if (row_zeros[i] == n && col_ones[j] == n-1)
                     printf("position (%d, %d)\n", i, j);
            }
}

- Miguel Oliveira September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

isnt this time complexity O(n^2 + n^2)

- Norman October 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

constants are ignored in big-O notation. thus O(n^2 + n^2) = O(n^2)

- Miguel Oliveira October 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oh lol, it equals 2n^2..duh. Thanks

- Norman October 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Lol .. no there cant be a row with all 0's and column with all 1's in same matrix.. avoid jumping into coding..

- hotcoder September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

read the question till the end "3)(i,j)th entry of the matrix can be either 0 or 1"

- Miguel Oliveira September 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

equivalent to the celebrity problem

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

Can you please provide a solution for those of us who don't know "the celebrity problem"? Thank you so much!

- anotherguy September 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

in celebrity problem, i must equals j, this one doesn't have such an assumption.

- Will September 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@anup : I understand there are two separate loops for rows and colums...

but, when you are traversing only rows, we have to use two nested loops to traverse each element in each row?

- surajsonawane111 September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Time Complexity : O( M * N )

Step 1 ) Store sum of each row and each column.
Mark all row/columns with sum 0 or 1 as ZERO or ONE.
If sum is 1, store what index had element 1.

Step 2 ) Iterate through all rows marked ZERO/ONE.

a) If a row is ZERO and a column marked ZERO/ONE exists, Bingo! we have our pair.

b ) If a row is ONE then check if the column corresponding to stored index is marked ZERO/ONE. If yes, we have our pair

keep repeating this process upto last row. if no success, no such row/column exists.

- perlscar September 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Cool, you were thinking just like I was :)

But you made several mistakes.
Columns need to be checked for sum of (n-1) and n.

Also, taking sums is a waste of a sweep down the rows/columns. It requires multiple sweeps if the sums are (n-1) for a column or 1 for a row.

And the last process is explained inefficiently. See my solution below.

- bigphatkdawg September 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Posting solution on behalf of Abhishek.

Here the idea is to traverse from the bottom to top.

1) consider a[i][j], check if i'th row is zero and j'th column is 1. if yes, return true. Note that we have started from a[n][n];
2) else if a[i][j] ==0
move one column left and check (1).
if a[i][j]==1
move one row up and check(1)..


In this code isrow checks if all the row element are 0's except a[i][j] and iscolumn checks if all the column elements are 1's except a[i][j].

isbooleanmatrix(A,i,j,n)
{
   if(i==0 && j ==0)
   {
       if(isrow && iscoulumn)
           return true;
       else
        {
              printf("Not Present");
              return;
	}
   }
   else
   {
           if(isrow && iscoulumn)
               return true;
          else  
          {
                if(A[i][j] == 0 && j)
                     isbooleanmatrix(A,i,j-1,n);
                else
                     isbooleanmatrix(A,i-1,j,n);
                 if(i)
                      isbooleanmatrix(A,i-1,j,n);
                 else
                      isbooleanmatrix(A,i,j-1,n);
	}
    }
}

- Harjit Singh September 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(i=0;i<n;i++)
{
  if(a[i][i]==1 && a[i][n-i-1]==1)
     t[index++]=i;
}
for(i=0;i<index;i++)
{
   flag=0;
   for(j=0;j<n;j++)
   {
      if(a[j][t[i]]!=1) 
      {
           flag=1;
           break;
       }
   }
   if(flag==0)
     return i;
}
return -1;

However, it is still O(n^2) if the matrix is dense with many entries 1.

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

it is not possible to have simultaneously all the elements of any row zero and also all elments of any one columns as 1...so no such i and j exist together....

- amit September 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please explain your idea first (or just your idea).
You have to touch every element of the array, O(n^2), so this gives us a lot of room to work with.

Say you have two integer arrays (zero initialized) of size n:
R[i] -> for information on each rows i
C[i] -> for information on each column i

As you are scanning row i from left to right, if you encounter a '1' somewhere but do not encounter it after that point, mark the position in R[i]. If you did not encounter any 1's mark this somehow (e.g., R[i] = -1). If any other case leave R[i] as it is (i.e., 0).

Do the same thing with each column ( mark position of the only 0 you saw in a column in C[i], or if you did not see any 0's, set C[i]=-1).

Now do your check:
If any element of R is > 0, say R[t], then if t's column was all 1's we have a match ... thus check of C[R[t]] = -1. We have a match if so.

If any element of C is > 0, say C[t], then if t's row was all 0's we have a match.. thus check R[C[t]] = -1. We will have a match.

Note, these check only takes O(n) time.
Going through each column and row only takes O(n^2) time.
Thus total O(n^2).

If interviewer expects somtehing that is n^2 without the O , then he/she is a loser.

Everything here is about 2n^2 + O(n). So why bother coming up with fancy/trickery? A systematic solution gives the best case time.

- bigphatkdawg September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can't we have two array coloumnSums and rowSums.
Then look for indexes where
(
i coloumnSums value is 0
and
j rowSums value is n-1
and
A[i][j] is 0
) OR (
i coloumnSums value is 1
and
j rowSums value is n
and
A[i][j] is 1
)

Time complexity O(2n)

- Abhi September 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def magic_row_col_exists(matrix):

    row_size = len(matrix)
    col_size = len(matrix[0])

    # dict to track which row/col is invalid
    invalid_rows = {}
    invalid_cols = {}

    num_zeroes_in_row = [0] * row_size
    num_ones_in_col = [0] * col_size

    # iterate all elements in the matrix
    for i in xrange(0, row_size):
        # if either all rows are invalid or all cols are invalid, return False
        if len(invalid_cols) == col_size or len(invalid_rows) == row_size:
            return False

        for j in xrange(0, col_size):
            if matrix[i][j] == 0:
                num_zeroes_in_row[i] += 1
            else:
                num_ones_in_col[j] +=1

            # mark if this row is invalid
            if j > 1 and num_zeroes_in_row[i] < j:
                invalid_rows[i] = True

            # mark if this col is invalid
            if i > 1 and num_ones_in_col[j] < i:
                invalid_cols[j] = True

    if len(invalid_rows) == row_size or len(invalid_cols) == col_size:
        return False
    else:
        # valid row indices are those which are not in invalid_rows
        valid_row_indices = [ i for i in xrange(0, row_size) if i not in invalid_rows.keys():

        for i in valid_row_indices:
            if num_zeroes_in_row[i] == row_size:
                # if a row containing all zeroes, as long as there is a col that is valid, return True
                if len(invalid_cols) < col_size:
                    return True
            else:
                # if a row contains all zeroes, except 1
                for j in xrange(0, col_size):
                    # look all the values in the column containing 1, they all must contain 1's
                    # if so, return True 
                    if matrix[i][j] == 1 and num_ones_in_col[j] == col_size:
                        return True
        return False

- aji.p3ndu September 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem could be simplified to : "find out if a boolean matrix NxN has a row of 0s with O(N) steps". Not possible without having a vector logical operation between rows/columns as a single step.

- celicom October 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you go through two consecutive rows , you will either find a 1 or not. Which is what we have to do here right ?
I mean the matrix will be say :
0 0 0 0
0 0 0 1
0 0 0 1
0 0 0 1

or say

0 1 0
0 1 0
0 1 0

In either case if we check two consecutive rows, then we have our result right?

- anirudh.mathad January 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it's not enough to check just 2 consecutive rows.
in the following example, only row 4 and column 3 match the problem's requirements, but there is no way to "guess" that we need to look up row 4. we need to check all rows

0 0 1 1 0
0 1 1 0 0
1 1 1 0 0
0 0 1 0 0
0 0 1 0 1

- Miguel Oliveira January 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oh ok. you are right. I assumed 1) and 2) to be already exists for all elements. But we have to find whether there exists such a row and col.

- anirudh.mathad January 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

O(n^2) or O(n) what is the meaning of "n"?

- maxiaophp September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 vote

If there is a row with all zeros, then there can't be a column with all 1s.

- math.matt September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I aggree..

- AM September 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

see the 3rd step, the (i,j)th entry of the matrix can be either 0 or 1

- thebiker925 September 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

int row, col;
printf("Enter row number: ");
scanf_s("%d", &row);

printf("\nEnter column number: ");
scanf_s("%d", &col);

if(row > n || col > n)
{
printf("\nIncorrect row/col value!");
}
else
{
int flag = 0;
for(int i = 0; i<n; i++)
{
if(matrix[row-1][i] != 0 && i!=col-1)
{
flag = 1;
break;
}
if(matrix[i][col-1]!=1 && i!=row-1)
{
flag=1;
break;
}
}

if(flag == 0)
{
printf("Row and Column values are correct!!!!!");
}
else
{
printf("Row and Column values are not correct---");
}
}

- Abhishek September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are not supposed to verify a given row/column pair. you need to check if one exists

- perlscar September 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

simple ,
create the array of all 0 in it and "OR" it with all the rows if the result is all zero then such a row exists.
again for column create array of 'n' 1 and "AND" it with all the rows till we find the result as all 1 array or end of column.

- Anonymous September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

1. Traverse the given matrix from top left to bottom right.
2. Keep track of the row. If row has all zeros and only one ambigous elemtent.
Save AMBIGUOUS_ROW[i] = Column Index of ambiguous elem. i is the row index we are conidering.
3. If row is all zeros. Mark AMBIGUOUS_ROW[i] as -2.
If not mark AMBIGUOUS_ROW[i] = -1 to indicate invalid row.
4. Perform the same with all columns marking row indices of all ambiguous elements. However check for 1s now. These are two seperate loops outside each other complexity is still O(n).
// We now have both AMBIGOUS_ROW[] and AMBIGOUS_COL[]
5. Traverse AMBIGUOUS_ROW from 0 to N-1. Each element with index 'i' in this array represents the ith row.
6. If AMBIGOUS_ROW[i] == -1 ignore. // Invalid row
7. If AMBIGOUS_ROW[i] == -2 ignore // All potential matches will be caught while traversing AMBIGUOUS_COL // All 0 row
8. If AMBIGOUS_ROW[i] == Something else, 5 for example, then see AMBIGOUS_COL[i]. If its -2, we have a match, otherwise we dont.
9. Now repeat for cols.
10. Traverse AMBIGUOUS_COL from 0 to N-1. Each element with index 'j' in this array represents the jth column.
11. If AMBIGOUS_COL[j] == -1 ignore.
12. If AMBIGOUS_COL[j] == -2 ignore // All potential matches will be caught while traversing AMBIGUOUS_ROW
13. If AMBIGOUS_COL[j] == Something else, 10 for example, then see AMBIGOUS_ROW[j]. If its -2, we have a match, otherwise we dont.

- Anup V. Saumithri September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Small Mistake Please Disregard. See the new one...

- Anup Saumithri September 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

PLEASE DISREGARD. SEE THE CODE BELOW...

- Anup Saumithri September 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

1. Traverse the given matrix from top left to bottom right.
2. Keep track of the row. If row has all zeros and only one ambiguous element.
Save AMBIGUOUS_ROW[i] = Column Index of ambiguous elem. i is the row index we are considering.
3. If row is all zeros. Mark AMBIGUOUS_ROW[i] as -2.
If not mark AMBIGUOUS_ROW[i] = -1 to indicate invalid row.
4. Perform the same with all columns marking row indices of all ambiguous elements. However check for 1s now. These are two separate loops outside each other complexity is still O(n).
// We now have both AMBIGOUS_ROW[] and AMBIGOUS_COL[]
5. Traverse AMBIGUOUS_ROW from 0 to N-1. Each element with index 'i' in this array represents the ith row.
6. If AMBIGOUS_ROW[i] == -1 ignore. // Invalid row
7. If AMBIGOUS_ROW[i] == -2 ignore // All potential matches will be caught while traversing AMBIGUOUS_COL // All 0 row
8. If AMBIGOUS_ROW[i] == Something else, 5 for example, then see AMBIGOUS_COL[5]. If its -2, we have a match, otherwise we dont.
9. Now repeat for cols.
10. Traverse AMBIGUOUS_COL from 0 to N-1. Each element with index 'j' in this array represents the jth column.
11. If AMBIGOUS_COL[j] == -1 ignore.
12. If AMBIGOUS_COL[j] == -2 ignore // All potential matches will be caught while traversing AMBIGUOUS_ROW
13. If AMBIGOUS_COL[j] == Something else, 10 for example, then see AMBIGOUS_ROW[10]. If its -2, we have a match, otherwise we dont.

- Anup Saumithri September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In step 2 : "Keep track of the row. If row has all zeros and only one ambiguous element.... : this takes O(n ^ 2) "

Please correct me if I am wrong. Maybe I misunderstood step 1.

- Arian September 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

right, it is O(n^2)

- Miguel Oliveira September 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Right I see why its O(n^2). This is probably not the solution what they are looking for. May be its not possible at all with O(n) complexity.

- Anup Saumithri September 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Traverse the matrix diagonally.

1) traverse each diagonal element.
2) while traversing, check if a[i-1][j]==0 && a[i][j-1]==1;
3) if (2) satisfies then i and j could be the row and column we are looking for. Traverse the entire column up and down and check if all are ones, then traverse the entire row left and right and check if all are zeros.
4) If still we don't get the row and column , traverse diagonally till the end repeating (2) and (3)

- Harjit Singh September 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This won't work. Simple counter-example:
1110
0000
0010
0010

- Miguel Oliveira September 12, 2013 | 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