PPractick

DSA interview question

Sudoku Solver

Hard · Algorithms · Topics: recursion, backtracking

The problem

A puzzle app lets users photograph a newspaper Sudoku and wants the solution filled in. Each row, column and 3 × 3 box must contain the digits 1–9 exactly once, and the given digits can't change. The classic approach is backtracking: pick an empty cell, try each digit that doesn't clash with its row, column or box, recurse, and undo on failure. Two refinements make it fast. Bitmasks per row, column and box make the clash check O(1), and always filling the empty cell with the fewest candidates first (the "minimum remaining values" heuristic) cuts the search dramatically.

Example: the LeetCode board beginning 53..7.... / 6..195... → its unique solution beginning 534678912 / 672195348.

Function signature

void solveSudoku(vector<vector<char>>& board);  // '.' marks empty cells; solved in place

Test cases

InputOutputExplanation
The LeetCode 37 example board534678912 / 672195348 / 198342567 / 859761423 / 426853791 / 713924856 / 961537284 / 287419635 / 345286179The unique solution
The solution above with cell (0, 0) blanked5 placed at (0, 0)Only one digit fits
An empty boardAny valid completed gridMany solutions; the solver returns one

(Confirm: the input has at least one solution; givens must not change; the board is 9 × 9 with '.' for blanks.)

How interviewers phrase it

Clarifying questions to ask

  1. Is a solution guaranteed to exist? (Yes, in LeetCode's version.)
  2. Should the board be modified in place?
  3. Is any valid solution acceptable if several exist?

Follow-ups to expect

  1. "How do you check a digit is allowed in O(1)?"
  2. "Which cell do you fill next?"
  3. "How do you undo?"
  4. "What's the complexity?"
  5. "More advanced techniques?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in