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 2 vote

int isValid(int (*sudoku)[SIZE],int row,int col)
{
	int i,j,start_row,start_col;
	// initialise a hash of size 10 with each slot containing 3.

	for(j=0;j<col;j++)
	for(i=0;i<SIZE;i++)// checking each column
		if(hash[sudoku[i][j]]==3)
			hash[sudoku[i][j]]--;
		else return 0;
	
	for(j=0;j<row;j++)		
	for(i=0;i<SIZE;i++)// checkin each row
		if(hash[sudoku[j][i]]==2)
			hash[sudoku[j][i]]--;
		else
			return 0;
		
	return 1;
}

If all the rows & all the columns have been checked for correctnes, then there is no need to check correctness of each grid.

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

I believe your last optimization is wrong: assume a correct configuration. Now swap two rows at the boundary of the top grid (could be any grid boundary). All rows and columns are going to be correct as before but the grid constraint is now incorrect (assuming the swapped rows are not identical)

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

public boolean isValid(int[][] m) {
int[][] t = new int[3][3];
int[] row = new int[10];

for (int i = 0; i < m.length; i++) {
int col = 0;
for (int j = 0; j < m.length; j++) {
/* assert value */
if(m[i][j] < 1 || m[i][j] > 9)
return false;
col += m[i][j];
t[i / 3][j / 3] += m[i][j];
row[i] += m[i][j];
if (col > 45 || t[i / 3][j / 3] > 45 || row[i] > 45)
return false;
}
}
return true;
}

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

acc. to me a simple soln would be. making a hasmap 9 hashmaps. per row. then 9 hashmaps per column and then 9 hashmaps per block. if we get any hashmap out of 27 hashmaps of those with lenght less than 9. then the soln is wrong. this is of complexity O(n) as no. of elements are 27 and we have 27 insert operations. we are making hashmaps of key as the value in cell and value can be left blank, in python this can also be done with the helps of sets.

- Anonymous July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

edit- you could also put in value of row col in value in hashmap.

- Ashish July 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

boolean isValidAns(int[][] sudoku){
    //Check the correctness for each row and column
    for (int i = 0; i< sudoku.length; i++){
        HashMap<Integer, Boolean> row = new HashMap<Integer,Boolean>();
        HashMap<Integer, Boolean> column = new HashMap<Integer,Boolean>();
        for (int j = 0; j< sudoku.length; j++){
            if( row.get (sudoku[i][j]) | | column.get( sudoku[j][i])){
                  return false;
            }else{ 
                  row.put( sudoku[i][j], true);
                  column.put(sudoku[j][i], true);
           }
        }
    }
   
    return true;
}

- emma.wang July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A sudoku block can contain numbers from 0 to 9 . Convert all the numbers into bit feilds enable numbers .
1 == 0x0000 0001
2 == 0x0000 0002
3 == 0x0000 0004
.
.
9 == 0x0000 0200

SUM == 0x0000 02FF == ( bit-wise OR every number)

Guarentees uniqueness + FAST !

- amit December 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

A simple solution would be
1. Make an array of n+1 size, with all elements initialized to zero
2. Scan first row and for every element, increase its count, then check the array if any element has count!=1, return false. Repeat the same for every row
3. Repeat the same for every column

At the end return true

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

Time Complexity - O(n^2)
Space - O(n)

- Luv July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you forgot the 3rd rule for sudoku, where you have to consider 9 3x3 squares.

- Anonymous July 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

this could be done without using the extra space as we know the sum of no in rows and columns and the no are also consecutive in rows and column as well as in 3x3 squares.

- hubulu July 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you only concentrate over sum, then following suduku will also get passed with your approach.

1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1

Thus, minimum O(n) space is required to see if all elements are present or not in a row, column or 3x3 sub matrix.

- anujarora July 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

find the sum of all the rows and columns..and the 3*3 matrix.!
it shud be constant

- lilprogrammer July 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It sounds good

- naves July 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your solution will be wrong if all the squares have the same number

- DashDash July 21, 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