RECURSION & SEARCH / ALGORITHM BRIEF

N-Queens

The N-Queens problem places n queens on an n x n chessboard so that no two attack each other: no shared row, column, or diagonal.

AdvancedPhase 06 / Topic 5 of 6Mental modelComplexityEdge cases
01

Overview

The N-Queens problem places n queens on an n x n chessboard so that no two attack each other: no shared row, column, or diagonal. It is the classic constraint-satisfaction backtracking problem.

Place one queen per row. For each row, try each column that is not attacked, recurse to the next row, and remove the queen when backtracking. Tracking attacked columns and diagonals in sets (or bitmasks) makes each safety check O(1). The key insight: all cells on one diagonal share row - col, and all cells on one anti-diagonal share row + col.

Seating rivals at a banquet

You seat one guest per row of tables. Each rival glares along their row, column, and both diagonals. You seat row by row, and when a row has no safe seat, you go back and move the previous guest.

02

When to use it

  • Placement problems with conflict rules between items.
  • Learning how to track constraints efficiently during backtracking.
  • Any problem where you fill a grid row by row with restrictions.
03

Problem patterns it solves

Row-by-row placement

Recognize it when: one item per row with column and diagonal conflicts.

  • 51. N-Queens
  • 52. N-Queens II
Grid constraint filling

Recognize it when: fill cells so rows, columns, and boxes stay valid.

  • 37. Sudoku Solver
  • 36. Valid Sudoku
Bitmask constraint tracking

Recognize it when: small n with fast conflict checks.

  • 52. N-Queens II (bitmask)
  • 1240. Tiling a Rectangle with the Fewest Squares
04

Where it is used in real software

Scheduling with conflicts

Assigning exam slots or rooms so conflicting items never share a slot is the same constraint-propagation idea.

VLSI and layout

Placing components so they do not interfere resembles queen placement with conflict rules.

Benchmarking solvers

N-Queens is a standard benchmark for constraint programming and SAT solvers.

05

Key terms

Column set
Columns already holding a queen.
Diagonal (row - col)
Constant along top-left to bottom-right diagonals.
Anti-diagonal (row + col)
Constant along top-right to bottom-left diagonals.
Bitmask
Integers whose bits mark attacked columns and diagonals.
06

How it works, step by step

  1. 1
    Place row by row

    Each row gets exactly one queen, so rows never conflict.

  2. 2
    For each column in the row

    Check cols, diag (r - c), and antiDiag (r + c) sets.

  3. 3
    Place the queen

    Add c, r - c, r + c to the sets and record the column.

  4. 4
    Recurse to the next row

    If r === n, a full solution is found.

  5. 5
    Remove the queen

    Delete from the sets and try the next column.

07

Solving 4-Queens

queens[r] = column of the queen in row r

Step 1 / 8
RowTry columnResultqueens
00place[0]
10, 1 attacked; 2place[0, 2]
20..3 all attackedbacktrack[0]
13place[0, 3]
21place[0, 3, 1]
3all attackedbacktrack to row 0[]
01place[1]
1..33, then 0, then 2place all[1, 3, 0, 2]

NOWRow: 0 | Try column: 0 | Result: place | queens: [0]

First solution: [1, 3, 0, 2], shown as .Q.. / ...Q / Q... / ..Q. . The mirror [2, 0, 3, 1] is the only other solution for n = 4.

08

Implementation

function solveNQueens(n) {  const result = [];  const queens = new Array(n).fill(-1);  const cols = new Set(), diag = new Set(), anti = new Set();   function place(r) {    if (r === n) {      result.push(queens.map((c) => ".".repeat(c) + "Q" + ".".repeat(n - c - 1)));      return;    }    for (let c = 0; c < n; c++) {      if (cols.has(c) || diag.has(r - c) || anti.has(r + c)) continue;      queens[r] = c;      cols.add(c); diag.add(r - c); anti.add(r + c);      place(r + 1);      cols.delete(c); diag.delete(r - c); anti.delete(r + c);    }  }  place(0);  return result;} // 52. N-Queens II with bitmasks: counts solutions very fastfunction totalNQueens(n) {  const full = (1 << n) - 1;  function count(cols, left, right) {    if (cols === full) return 1;    let total = 0;    let free = full & ~(cols | left | right);    while (free) {      const bit = free & -free; // lowest free column      free -= bit;      total += count(cols | bit, ((left | bit) << 1) & full, (right | bit) >> 1);    }    return total;  }  return count(0, 0, 0);}
09

Complexity and performance

TimeO(n!)

Upper bound; pruning makes it much faster in practice.

Safety checkO(1)

Sets, boolean arrays, or bitmasks.

SpaceO(n)

Constraint sets and recursion.

Solutions for n = 892

n = 4 has 2; n = 2 and 3 have none.

10

Trade-offs

Sets vs arrays vs bitmasks

Sets are readable; boolean arrays are faster; bitmasks are fastest and elegant but harder to read. All give O(1) checks.

Scanning the board for conflicts

Checking attacks by scanning is O(n) per placement; constraint sets remove that cost.

11

Variants and related techniques

Count only

N-Queens II returns the number of solutions; the bitmask version counts n = 14 quickly.

Symmetry reduction

Only try first-row columns in the left half and double the count (careful with odd n).

Sudoku

Same idea with row, column, and 3 x 3 box constraints and more choices per cell.

12

Common mistakes

  • Negative indexes for r - c.

    Fix: Offset by n when using arrays: r - c + n.

  • Forgetting to remove constraints on backtrack.

    Fix: Delete from all three sets after the recursive call.

  • Building strings inside the recursion.

    Fix: Build the board strings only when a full solution is found.

13

Interview questions

Why do r - c and r + c identify diagonals?

Moving down-right increases both r and c by 1, keeping r - c constant. Moving down-left increases r and decreases c, keeping r + c constant.

How would you speed up N-Queens II?

Use bitmasks for columns and both diagonals, extract free positions with bit tricks, and shift the diagonal masks when moving to the next row.

14

Practice problems

ProblemDifficultyWhat it trains
51. N-QueensHardConstraint sets.
52. N-Queens IIHardBitmask counting.
36. Valid SudokuMediumConstraint tracking.
37. Sudoku SolverHardBacktracking with three constraint types.