Valid Sudoku
Updated
Problem statement
A 9 x 9 Sudoku board is given as nine strings. board[r][c] is the cell in row r and column c (both 0-based). Each cell holds a digit from 1 to 9, or '.' when it is empty.
The board is valid when, looking only at filled cells:
- no row contains the same digit twice,
- no column contains the same digit twice, and
- none of the nine 3 x 3 boxes contains the same digit twice. The boxes are formed by rows 0-2, 3-5 and 6-8 crossed with columns 0-2, 3-5 and 6-8.
Return true if the board is valid and false otherwise. The board does not have to be full, and it does not have to be solvable: only repeated digits make it invalid.
Function
isValidSudoku(board: String[]) → booleanExamples
Example 1
board = ["5..6..9..",".7..9..4.","..8..2..7","8..7..4..",".2..5..9.","..3..4..6","9..5..2..",".8..1..3.","..5..6..9"]return = trueNo digit appears twice in any row, any column or any of the nine 3 x 3 boxes, so the board is valid. Empty cells (.) are ignored.
Example 2
board = ["5.76..9..",".7..9..4.","..8..2..7","8..7..4..",".2..5..9.","..3..4..6","9..5..2..",".8..1..3.","..5..6..9"]return = falseThis is Example 1 with one more digit: a 7 at row 0, column 2 (0-based). Row 0 and column 2 still have no repeated digit, but the top-left box already has a 7 at row 1, column 1, so the board is not valid.
Constraints
board.length == 9andboard[i].length == 9- Every
board[i][j]is a digit'1'to'9'or'.'.
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- Validate Rectangular Game PiecesGoogle · ONSITE INTERVIEWGoogle interview questions
- Query Whether an Island Size ExistsMeta · ONSITE INTERVIEWMeta interview questions
- Validate a Sudoku BoardZipRecruiter · ONSITE INTERVIEWZipRecruiter interview questions
- Check a Partly Filled Sudoku GridExpedia · ONSITE INTERVIEWExpedia interview questions
- Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Sparse Matrix MultiplicationByteDance · PHONE SCREENByteDance interview questions
- First Completed Mingo LineEpic · OAEpic interview questions
- Valid SudokuVerkada · PHONE SCREENVerkada interview questions
- Count 2x2 Submatrices by Black CellsTikTok · OATikTok interview questions