Solve a Sudoku Puzzle In Place With Constraint-Guided Backtracking

Last updated: November 29, 2026

Quick Overview

Given a 9 by 9 Sudoku grid with some cells empty, fill it in place so that every row, column and 3 by 3 box contains the digits 1 to 9 exactly once. Return False if the givens are invalid or no solution exists.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given a 9 by 9 Sudoku grid with some cells empty, fill it in place so that every row, column and 3 by 3 box contains the digits 1 to 9 exactly once. Return False if the givens are invalid or no solution exists.

A Meta onsite uses Sudoku to see constraint-satisfaction backtracking with good heuristics, not just brute force over 81 cells. Interviewers look for constant-time validity checks via row, column and box sets, and appreciate the minimum-remaining-values choice.

What the Interviewer Expects
  • Index boxes as (r // 3) * 3 + c // 3
  • Precompute used digits per row, column and box
  • Validate the givens before searching
  • Pick the most constrained empty cell next
  • Undo board and set changes on failure
Key Topics to Cover
Backtracking
Constraint satisfaction
Heuristics
Hash set
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you detect whether a puzzle has a unique solution?
  • How would you generate a puzzle with exactly one solution?
  • How would you speed this up with bitmasks or Dancing Links?
  • How would constraint propagation (naked singles) reduce search?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Board is a 9x9 list of lists of characters, . for empty.
  • Modify in place; return True when solved.
  • Invalid givens or unsolvable puzzles return False....
High-Level Design

Record the digits present in each row, column and box. Collect empty cells. At each step, scan empty cells and pick the one with the fewest candidate digits (candidates are 1 to 9 minus the three used...


Submit Your Answer
Markdown supported