‹ DS&A interview · Socratic
DSA · Backtracking · #127

N-Queens II

Module 52 · difficulty 4/5·30:00starts on first keystroke

The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other (no two share a row, column, or diagonal). Given an integer `n`, implement `totalNQueens(n)` that returns the **number of distinct solutions** to the n-queens puzzle.

Examples
  • n = 4 2There are two distinct solutions to the 4-queens puzzle.
  • n = 1 1A single queen on a 1x1 board.
  • n = 8 92
Constraints
  • · 1 <= n <= 9
  • · The board is exactly n x n.
  • · Count solutions, do not enumerate boards.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.