Amazon Interview Question for Software Engineer / Developers






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

for (i=0;i<m;i++)
{
 for(x=0;x<row.length;x++)
 {
  if(row[x]==i) flag =1;
 }
 for (j=0;j<n;j++)
 {
   for(x=0;x<col.length;x++)
 {
  if(col[x]==j) flag =1;
 }
  if(!(flag = 1))
   if(m[i,j]==0)
    {
    for(l=0;l<n;l++)
      {
        m[i,l]=0;
      }
    for(l=0;l<m;l++)
      {
        m[l,j]=0;
      }
    }
    }
   row[x++]=i;
   col[y++]=j;
   flag=0;
 }
}

- Hiren January 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question not that easy as it seems.
@Hiren - your solution may seem obvious one but it is not.
Let say I have this matrix
0 2 3
0 2 3
0 2 2

Now for sure you will make the first column and row 0 and next time you wont consider those rows and cols again, but the problem is row 2 and 3 both initially had 0s so even those rows should be zero.

- TripleH January 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Keep a bitmap of size (m + n).
2. Scan the matrix.
3. If you find 0, set the respective 2 bits in the bitmap (one for row and one for column).
4. Now use the information in the bitmap to write zeros in the expected rows and columns in the matrix.

- Anonymous January 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain what is a bitmap. and how would we use bitmap in this question. Thanks In advance

- addy January 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int array[m][n];
int x=0; int row[m];
int y=0; int row[n];
for(int i=0;i<m;i++){
    for(int j=0;j<m;j++){
        if(array[i,j]==0){
        row[x++] = i; col[y++]=j; 
        }
    }
}
for(int a=0;a<x;a++){
    for(int z=0;z<n;z++){
        array[row[a],z] = 0;
    }
}
for(int b=0;a<y;b++){
    for(int w=0;w<m;w++){
        array[w,col[b]] = 0;
    }
}

Not sure if this is the most efficient way thou.. Feel free to comment

- XeqtR January 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Line 5: j<n

- XeqtR January 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And array syntax; OMG too many syntax errors; array[i][j]

- XeqtR January 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry.. Small typo in 3rd line;
It should be 'int col[n]'

- XeqtR January 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void changeMatrix(int[][] input, int row, int col)
{
//scan through the matrix to get 0
for(int i=0;i<=row;i++)
{
for(int j=0;j<=col;j++)
{
if(input[i][j]==0)
{
convertRowandCol(input,i,j,row,col);

}
}
}


}

private static void convertRowandCol(int[][] input, int i, int j, int row,
int col) {

for(int x=0;x<col;x++)
{
input[i][x]=0;
}

for(int y=0;y<row;y++)
{
input[y][j]=0;
}
}

comlexity : o(m*n) for scanning the matrix
o(m +n) for again changing matrix.
Not efficient but simple.

- Anonymous January 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your logic do not work. Please check for this input.
{{0,1,1},
{1,0,1},
{1,1,1},
{1,1,1}}

what you are going to get is...
{{0,0,0},
{0,0,0},
{0,0,0},
{0,0,0}}

We suppose to get...
{{0,0,0},
{0,0,0},
{0,0,1},
{0,0,1}}

Thanks
Robo

- Robo March 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// find the positions of 0s
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (matrix[i][j] == 0) {
row[rowCount] = i;
rowCount++;
col[colCount] = j;
colCount++;
}
}
}

int i = 0;
int rowNo = 0, colNo = 0;
while (row[i] != -1 && col[i] != -1) {
rowNo = row[i];
colNo = col[i];

// make upper column 0
for (int r = 1; r <= rowNo; r++) {
matrix[rowNo - r][colNo] = 0;
}
// make lower column 0
for (int r = 1; r < (height - rowNo); r++) {
matrix[rowNo + r][colNo] = 0;
}
//make the row 0
for (int c = 0; c < width; c++) {
matrix[rowNo][c] = 0;
}
i++;
}

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

row string and col string with AND of all variables per row or column.

2nd round make it 0.
if its row in row and col in col for 1, make it 1
otherwise keep 0

- Anonymous March 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you please explain your logic with sample input?

Thanks
Robo

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

Preprocessing: Fill up two arrays Row[] & Col[] such that Row[i] = 1 & Col[j] = 1 if matrix[i][j] = 0
Use Row[] & Col[]
if(Row[i]Col[j] == 1)
matrix[i][j] = 0

To save space Row & Col could be array of bits

- M August 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication3
{


class test
{
static int row = 0;
static int col = 0;
public static void Main(string[] args)
{
int[,] a = { { 11, 2, 3, 1 }, { 4, 1, 6, 5 }, { 3, 6, 2, 4 } };
row = a.GetLength(0);
col = a.GetLength(1);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (a[i, j] == 1)
{
markarraywithzero(a, i, j);
break;
}
}
}

}

public static void markarraywithzero(int[,] a, int rowitem, int colitem)
{

for (int i = 0; i < col; i++)
{
a[rowitem, i] = 0;
}
for (int i = 0; i < row; i++)
{
a[i, colitem] = 0;

}
}
}
}

- Anonymous December 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//C#
static void setZeros(int[,] _arr, int _iMax, int _jMax)
        {
            int[] rows = new int[_arr.GetLength(0)];
            int[] colums = new int[_arr.GetLength(1)];

            for (int i = 0; i < _iMax; i++)
            {
                for (int j = 0; j < _jMax; j++)
                {
                    if (_arr[i, j] == 0)
                    {
                        rows[i] = 1;
                        colums[j] = 1;
                    }
                }
            }

            for (int i = 0; i < _iMax; i++)
            {
                for (int j = 0; j < _jMax; j++)
                {
                    if ((rows[i] == 1 || colums[j] == 1))
                    {
                        _arr[i, j] = 0;
                    }
                }
            }
        }

- anup.h.nair December 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;


public class MultiplyExceptSelf {

public static void main(String[] args) {

int arr[][] = { {1,0,1,0},
{1,0,1,0},
{1,0,0,0},
{0,0,0,0}
};
ArrayList<Integer> xcor = new ArrayList<Integer>();
ArrayList<Integer> ycor = new ArrayList<Integer>();


for(int i=0;i<4;i++){

for(int j=0;j<4;j++){

if(arr[i][j] == 1){

if(xcor.contains(i)){

}
else{
xcor.add(i);
}
if(ycor.contains(j)){

}
else{
ycor.add(j);
}

}
}
}
System.out.println(xcor);
System.out.println(ycor);

for(int i=0;i<xcor.size();i++)
{
for(int j=0;j<4;j++){
arr[xcor.get(i)][j] = 1;
}
}

for(int i=0;i<ycor.size();i++)
{
for(int j=0;j<4;j++){
arr[j][ycor.get(i)] = 1;
}
}
for(int i=0;i<4;i++){

for(int j=0;j<4;j++){
System.out.print(arr[i][j]);
}
System.out.println("\n");
}

}

}

- Learner September 04, 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