Amazon Interview Question for Software Engineer / Developers






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

public int findMaximTrueEls(boolean[][] arr)
{
for(int i=arr.length-1;i>=0;i--)
{
for(int j=0;j<arr[0].length;j++)
{
if(arr[i][j])
{ return i;
}
}
}
return -1;
}

You could also perform binary search on each row if you need to achieve the best average run time.

- kiruba June 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const int M = 100;
const int N = 100;

int findRow(bool matrix[M][N])
{
int i, j, row;
i = j = row = 0;

while (i < M && j < N)
if (matrix[i][j])
{
row = i; ++j;
} else ++i;

return row;
}

// time complexity is O(M+N)

- fiddler.g June 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Loony,

Can you please post an example..

- DashDash June 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 1 1 1 0 0 0
1 1 0 0 0 0 0
1 1 1 0 0 0 0
1 1 1 1 1 0 0
in this case should be ans is 3(index 0)???

- ashish.cooldude007 June 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ya... I guess this is what Loony meant

- gradStudent June 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

then we can do it easily by making a array and storing no of 1's as count and can get max no ..

- ashish.cooldude007 June 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes it is one of the solutions but its not optimum...

- Harit June 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Following apporach time complexity is o(n), binary search can be used to further optimize it
//When return -1 means no row has true value
public int maxtrueRow(boolean val[][], int m, int n){
int row = -1, i=0, j=0;
while(i < m && j < n){
if(val[i][j]){
j++;
row = i;
}else{
i++;
}
}
return row;
}

- boyjemmy June 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, the time complexity for this is O(m+n)

- boyjemmy June 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//The worst case time complexity for this approach is O(mlogn), so it suits when m << n
//When return -1 means no row has true value
public int maxtrueRowBS(boolean val[][], int m, int n){
int row = -1, i=0, j=0;
while(i < m && j < n){
//BS finds the first false in row i of val starting col j
//If no false value found, return -1;
int k = BS(val, i, j);
if(k == -1) {row = i; break;}
if(k > j) { j = k; row = i;}
i++;
}
return row;
}

- boyjemmy June 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algo:

Start with the last column.
Add all the elements of the column.

if the summation is == 0, select a column to left.and add all the elements of that column.
repeat above step, till you find a column with summation value > 0.
here you found out how many 1s does the rows with max 1s have.
to find our what is the row number, there one trick we can apply while adding all the elements of the column.
The trick is: value of the 1st row is multiplies by 1.
value of the 2nd row is multiplied by 2.
and so on.

so when we find the summation value > 0, the value of the summation gives us row with the max 1s. (1 based index).

worst case O(M*N)

- morpheus July 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if there are two rows with maximum ones then we cannot use the trick that you mentioned.

- Dsk July 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int maxRow(int arrayOfBool[][cols],int rows,int cols)
{

int i = 0 ,j = 0;

for(j=cols-1;j>=0;j--)
{
for(i=0;i<rows;i++)
{
if(arrayOfBool[i][j])
return (i+1);

}
}

return 0;
}
//worst case tc O(rows*cols) case: array with all zeroes
//best case O(1) // case array[rows-1][cols-1] = 1

- KrishKrish July 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int maxRow(int arrayOfBool[][cols],int rows,int cols)
{

int i = 0 ,j = 0;
int maxR = -1;
while(i<rows && j<cols)
{
if(arrayOfBool[i][j])
{
maxR = i;//mar current row as the max row
j++; // go to next col
}
else // if u find zero , go to next row, keep the col num intact
i++; // go to next row
}
return maxR;
}

- KrishKrish July 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

An array of boolean is stored continguously in memory. Say it's 17 by 10, it uses 170 bits of contiguous data.
I think the proper answer would take into account machine architecture and significantly improve performance by operating with Words (32 bits) rather than individual bits. This strategy is only valid if processing >= on 32 bits one time is faster than on individual bits 32 times. I believe that this is the case.

Therefore, one would need to employ two algorithms. If the number of columns is less than 32, then only algorithm #2 is used. if the number of columns is a multiple of 32, only algorithm #1 is used, and if it's anything else, use #1 for the first x multiples of 32 bits, then use algorithm #2.

An alternative would be to resize the array to the next 32-bit multiple. This may be effective depending on what data you anticipate getting.

Algorithm #1 would be as follows:
1. Compare the first 32 bits of each row moving down one at a time. As soon as you reach a number which is maxInt, move right to the next chunk of 32 bits and continue to compare rows moving down one at a time, moving right whenever you reach maxInt. You stop when you reach the bottom, and your answer is the largest value.

There is no technical change in algorithm performance using big O notation, but in practice it is n/32 + m. Or, way faster. Worst case = algorithm 2, which is the bit-by-bit strategy.

- SomeGuy July 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int FindRow( int** Matrix , int R , int C )
{
    int r=0,c=0;
    
    int MaxTrue=0;
    int MaxRow=-1;
    
    while( r<R && c<C )
     {
           if( Matrix[r][c]==1 )
             c++;
           else
             {
                 if( MaxTrue < c ) /* 0 Indexed */
                  {
                      MaxTrue=c;
                      Maxrow=r;
                  }
                     r++ ;  
                     
                     
                     }
                     
                     
                     }
                     
                     
               if( c==C )return r;
                     
                     return MaxRow;
                     
                     }

- LOLer July 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

rows are always sorted 1s on left and 0s on right (means decresing) so you can apply binary search to find index where array element turing 1to0, (logQ) (of PxQ matrix)
you have to do it for every row(P)and find maximum.
this can be done in O(P*logQ), with O(p) space.

- Anonymous July 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can go column wise
for eg
111000
110000
111100
000000
so in this case if we move column wise then we can get the ans
scan 1st row till 0 is found. term it as max row.jump to 2nd row same column N chk if it is 1 then move righthand side row wise otherwise jump to next row.

- JiM.iiitm July 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(m*n) worst case, not the best

- windmaple July 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No m+n

- Sometr August 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No m+n

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

Easier way:
Sum elements of each row; and then find the max of it between rows, and return the index; it can be return using just one loop

- Mabby October 22, 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