Valid Sudoku
Hash Tables (Frequency Counter)
Asked at 12 companies
7 recent
- Apple
- Amazon
- Bloomberg
- Microsoft
- Meta
- tcs
- Confluent
- Samsara
- Wissen Technology
+2 moreShow fewer
- EarnIn
- Snap
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Counting subarrays with a map of totals
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Caches and duplicate detection: A hash map answers “have I seen this before?” in one step. It's how apps cache results, and how a photo app spots the same picture uploaded twice.
About this problem
Multiple hash sets for row/col/box validation
Try it in the editor below for 30 min: passing its tests counts as solved and starts your reviews. The full statement is on LeetCode.
Examples
Full statement on LeetCode ↗Example 1
- Input
- board = a 9×9 grid, partly filled (the classic sample board)
- Output
- true
Walk through it step by step(32 steps · shows the approach, so try first)
- A board is valid if no digit repeats in any row, column, or 3×3 box. We keep one set per row, per column, and per box, and check each filled cell against them.
- 5 at row 0, column 0: not yet in row 0, column 0, or box 0, so it's fine. Remember it in all three.
- 3 at row 0, column 1: not yet in row 0, column 1, or box 0, so it's fine. Remember it in all three.
- 7 at row 0, column 4: not yet in row 0, column 4, or box 1, so it's fine. Remember it in all three.
- 6 at row 1, column 0: not yet in row 1, column 0, or box 0, so it's fine. Remember it in all three.
- 1 at row 1, column 3: not yet in row 1, column 3, or box 1, so it's fine. Remember it in all three.
- 9 at row 1, column 4: not yet in row 1, column 4, or box 1, so it's fine. Remember it in all three.
- 5 at row 1, column 5: not yet in row 1, column 5, or box 1, so it's fine. Remember it in all three.
- 9 at row 2, column 1: not yet in row 2, column 1, or box 0, so it's fine. Remember it in all three.
- 8 at row 2, column 2: not yet in row 2, column 2, or box 0, so it's fine. Remember it in all three.
- 6 at row 2, column 7: not yet in row 2, column 7, or box 2, so it's fine. Remember it in all three.
- 8 at row 3, column 0: not yet in row 3, column 0, or box 3, so it's fine. Remember it in all three.
- 6 at row 3, column 4: not yet in row 3, column 4, or box 4, so it's fine. Remember it in all three.
- 3 at row 3, column 8: not yet in row 3, column 8, or box 5, so it's fine. Remember it in all three.
- 4 at row 4, column 0: not yet in row 4, column 0, or box 3, so it's fine. Remember it in all three.
- 8 at row 4, column 3: not yet in row 4, column 3, or box 4, so it's fine. Remember it in all three.
- 3 at row 4, column 5: not yet in row 4, column 5, or box 4, so it's fine. Remember it in all three.
- 1 at row 4, column 8: not yet in row 4, column 8, or box 5, so it's fine. Remember it in all three.
- 7 at row 5, column 0: not yet in row 5, column 0, or box 3, so it's fine. Remember it in all three.
- 2 at row 5, column 4: not yet in row 5, column 4, or box 4, so it's fine. Remember it in all three.
- 6 at row 5, column 8: not yet in row 5, column 8, or box 5, so it's fine. Remember it in all three.
- 6 at row 6, column 1: not yet in row 6, column 1, or box 6, so it's fine. Remember it in all three.
- 2 at row 6, column 6: not yet in row 6, column 6, or box 8, so it's fine. Remember it in all three.
- 8 at row 6, column 7: not yet in row 6, column 7, or box 8, so it's fine. Remember it in all three.
- 4 at row 7, column 3: not yet in row 7, column 3, or box 7, so it's fine. Remember it in all three.
- 1 at row 7, column 4: not yet in row 7, column 4, or box 7, so it's fine. Remember it in all three.
- 9 at row 7, column 5: not yet in row 7, column 5, or box 7, so it's fine. Remember it in all three.
- 5 at row 7, column 8: not yet in row 7, column 8, or box 8, so it's fine. Remember it in all three.
- 8 at row 8, column 4: not yet in row 8, column 4, or box 7, so it's fine. Remember it in all three.
- 7 at row 8, column 7: not yet in row 8, column 7, or box 8, so it's fine. Remember it in all three.
- 9 at row 8, column 8: not yet in row 8, column 8, or box 8, so it's fine. Remember it in all three.
- Every filled cell passed all three checks, so the board is valid.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: optimal solution
Solution code
Optimal solutionOne pass over the board. Remember which digits each row, column and 3×3 box has seen (box index = (r / 3) * 3 + c / 3); a repeat anywhere makes it invalid.
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
rows = [set() for _ in range(9)]
cols = [set() for _ in range(9)]
boxes = [set() for _ in range(9)]
for r in range(9):
for c in range(9):
d = board[r][c]
if d == '.':
continue
b = (r // 3) * 3 + c // 3
if d in rows[r] or d in cols[c] or d in boxes[b]:
return False
rows[r].add(d); cols[c].add(d); boxes[b].add(d)
return TrueReference solution, tested in Python, Java and C++.
Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.