Interview Question

Mobile Software Engineer Interview

-San Francisco, CA

Uber

Implement a Sudoko puzzle validator - given a 9x9 matrix of numbers (1-9) and "." for empty spaces, return true for a valid puzzle matrix and false if it would not be a valid sudoku puzzle.

AnswerAdd Tags

Interview Answers

3 Answers

1

Each iteration? You need just an iteration. Complexity o(n).

Anonymous on

0

my solution was still O(n) - 3n to be exact. i "iterate" over the entire matrix 3 times - once to check for duplicates in the rows, once to check for duplicates in the columns, and once to check for duplicates within sub matrices. During each of those iterations I visit each element of the matrix exactly once and just check whether it's already in a dictionary of seen numbers which would be a constant time operation, so this is in total would be O(n). I can't think of how to check all those in just one iteration , if that's what you were saying you could do. (at least without using O(n) extra space.. mine only uses constant extra space) i would appreciate if you could elaborate

could you maybe explain more what you mean? on

0

I basically just checked for duplicate numbers in each row, column, and then each 3x3 inner matrix. each iteration, i reset a dictionary of "seen" numbers. "." characters wouldn't matter, if i saw a number 1-9 i would check if it was already in the dictionary, if it was return false. return true at the very end if never encountered repeat numbers in rows, columns, or inner squares. when I explained this approach to the interviewer and he seemed satisfied with it although it was not easy to tell what the hell he was thinking or saying. i was working in a shared text file (i think it was codepen?), and even though the interviewer could have typed too, he didn't (even when i had to ask him repeatedly to spell a word i couldn't make out)..

Anonymous on

Add Answers or Comments

To comment on this, Sign In or Sign Up.