Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.isValidSudoku(board). board is 9 by 9 and contains digits 1-9 or '.'. Return whether no filled digit repeats in any row, column, or 3-by-3 box.

Starter code

class Solution:
    def isValidSudoku(self, board):
        pass
Test cases

valid-partial

{
  "args": [
    [
      [
        "5",
        "3",
        ".",
        ".",
        "7",
        ".",
        ".",
        ".",
        "."
      ],
      [
        "6",
        ".",
        ".",
        "1",
        "9",
        "5",
        ".",
        ".",
        "."
      ],
      [
        ".",
        "9",
        "8",
        ".",
        ".",
        ".",
        ".",
        "6",
        "."
      ],
      [
        "8",
        ".",
        ".",
        ".",
        "6",
        ".",
        ".",
        ".",
        "3"
      ],
      [
        "4",
        ".",
        ".",
        "8",
        ".",
        "3",
        ".",
        ".",
        "1"
      ],
      [
        "7",
        ".",
        ".",
        ".",
        "2",
        ".",
        ".",
        ".",
        "6"
      ],
      [
        ".",
        "6",
        ".",
        ".",
        ".",
        ".",
        "2",
        "8",
        "."
      ],
      [
        ".",
        ".",
        ".",
        "4",
        "1",
        "9",
        ".",
        ".",
        "5"
      ],
      [
        ".",
        ".",
        ".",
        ".",
        "8",
        ".",
        ".",
        "7",
        "9"
      ]
    ]
  ]
}

Expected: true

Wizard outline
  1. Step 1: Initialize Solution.isValidSudoku

    Replace the empty starter with the first real state owned by Solution.isValidSudoku. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Assemble the primary transition

    Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.

  3. Step 3: Pass the Valid Partial case

    Complete the readable core algorithm for one representative Interview case. Encode each cell's three constraint groups and reject repeated membership.

  4. Step 4: Harden the Box Duplicate boundary

    Repair the reviewed boundary and pass the complete submission contract. Each filled cell is inserted into exactly its row, column, and box sets. A repeated membership is exactly a Sudoku constraint violation; no repeat means all three constraint families hold.

Footguns and prerequisites
  • Box identity is (row // 3, column // 3), not row % 3 and column % 3.
  • hashing and sets
Reviewed references
Practice prerequisites
  • Track Previously Seen Values(opens in a new tab)

    Track Previously Seen Values isolates before processing index i, seen contains exactly the distinct values from indices smaller than i. That focused state discipline is required when implementing valid sudoku as a complete Interview Problem.

Recommended approach and implementation

Maintain one set per row, column, and box while scanning non-empty cells.

Why it works: Each filled cell is inserted into exactly its row, column, and box sets. A repeated membership is exactly a Sudoku constraint violation; no repeat means all three constraint families hold.

class Solution:
    def isValidSudoku(self, board):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        rows = [set() for _ in range(9)]
        columns = [set() for _ in range(9)]
        boxes = [set() for _ in range(9)]
        for row in range(9):
            for column in range(9):
                value = board[row][column]
                if value == '.':
                    continue
                box = (row // 3) * 3 + column // 3
                if value in rows[row] or value in columns[column] or value in boxes[box]:
                    return False
                rows[row].add(value)
                columns[column].add(value)
                boxes[box].add(value)
        return True