Valid Sudoku

MediumMatrixhashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are handed a partially filled 9 x 9 Sudoku grid board. Each cell holds a digit character '1'-'9' or '.' for an empty cell. Return true if the filled cells break none of the Sudoku rules, meaning no digit repeats within any row, any column, or any of the nine 3 x 3 sub-boxes. Return false otherwise. Only the digits already placed matter: a board can be valid even if it has no solution, and empty cells are ignored.

Examples

Input: board = [[".", ".", ".", ".", ".", ".", "7", ".", "."], [".", "5", "6", "7", ".", ".", "1", ".", "."], [".", ".", ".", "1", ".", "3", "4", "5", "6"], [".", "3", ".", ".", ".", ".", ".", ".", "1"], [".", ".", ".", ".", "9", "1", "2", "3", "."], ["8", ".", "1", ".", ".", "4", "5", ".", "."], [".", "4", "5", ".", ".", "8", "9", "1", "."], [".", "7", "8", "9", "1", ".", "3", ".", "5"], ["9", ".", "2", ".", "4", "5", "6", "7", "."]]
Output: true
Every placed digit appears at most once in its row, column, and 3x3 box.
Input: board = [["9", ".", ".", ".", ".", ".", "7", ".", "."], [".", "5", "6", "7", ".", ".", "1", ".", "."], [".", ".", ".", "1", ".", "3", "4", "5", "6"], [".", "3", ".", ".", ".", ".", ".", ".", "1"], [".", ".", ".", ".", "9", "1", "2", "3", "."], ["8", ".", "1", ".", ".", "4", "5", ".", "."], [".", "4", "5", ".", ".", "8", "9", "1", "."], [".", "7", "8", "9", "1", ".", "3", ".", "5"], ["9", ".", "2", ".", "4", "5", "6", "7", "."]]
Output: false
Same board, but the 9 now in the top-left corner repeats the 9 at the bottom of column 0.

Constraints

  • board.length == 9
  • board[i].length == 9
  • board[i][j] is a digit '1'-'9' or '.'

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems