Skip to content
AI360Xpert
Beta
Difficulty: HardBacktracking

N-Queens

Problem in Plain English

Place n queens on an n by n board so no two share a row, column, or diagonal, and return every distinct board of Q and dot strings. Layouts that break any attack rule are excluded.

Problem Statement

The n-queens puzzle asks for placements of n queens on an n x n board where no two queens share a row, column, or diagonal.

Given n, return all distinct valid placements in any order.

Write each board as n strings of length n, using Q for a queen and . for an empty square.

Constraints

  • 1 <= n <= 9

Examples

Example 1

Input
n = 4
Output
[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]

The column sequences [1, 3, 0, 2] and [2, 0, 3, 1] use distinct columns and have no pair with equal row and column gaps. Every other sequence violates an attack rule. Reflection produces a different board, so both layouts belong in the answer.

Example 2

Input
n = 1
Output
[["Q"]]

A lone queen on a 1 by 1 board attacks nothing.

Example 3

Input
n = 3
Output
[]

Starting in the middle column attacks every cell in the next row. Starting in an outer column forces the opposite outer column in row 1, leaving no safe cell in row 2. Thus every branch fails even though the board has three rows.

Intuition

With n queens and n rows, a safe board must put exactly one queen in each row. Represent a board by its chosen columns: [1, 3, 0, 2] means queens at (0, 1), (1, 3), (2, 0), and (3, 2). Distinct columns prevent vertical attacks. Two queens share a diagonal exactly when their row gap equals their column gap; equivalently, their row-plus-column or row-minus-column keys match. Backtracking explores a column choice, finishes its descendants, then removes its attack marks so the next choice starts from the same prefix.

Approaches

Guess Then Validate

Solution Details

Reveal Guess Then Validate: intuition, complexity, and code

Hints

Hint 1
Each row needs exactly one queen, so the whole puzzle is choosing one column per row.
Hint 2
A new queen at row r and column c is safe exactly when no earlier queen shares c, r + c, or r - c.
Hint 3
Track those three values in sets while descending row by row, and emit the board only when all n rows are filled.

Edge Cases

  • n = 1 accepts the single cell at once and returns [["Q"]].
  • n = 2 and n = 3 have no valid layout at all, so the answer is an empty list.
  • n = 9 fans out widely, which is why pruning dead rows early matters far more than board-copy speed.

Common Mistakes and Interview Tips

  • Checking only columns and rows while forgetting diagonals, which accepts boards where queens snipe along a slope.
  • Mixing up the two diagonal keys, such as using row + col for both, which blocks safe cells and frees attacked ones.
  • Removing a queen from the board but leaving its column or diagonal marked, which silently kills every later branch.
  • Saving the shared columns array as an answer makes later choices overwrite earlier solutions. Build a fresh board at every successful leaf.
  • Returning after the first solution misses other placements, including the reflected board for n = 4.
  • Interview tip: derive both diagonal keys from two coordinates, then explain why all three attack marks must be removed after recursion.

Key Takeaway

Constraint puzzles shrink when each choice is validated against the choices above it: track exactly what past decisions forbid and prune the moment a row has nothing legal left.