Skip to interview prep
Apexed
Problem list113 / 125

113. N-Queens (count solutions)

HardRecursion & backtracking

Return how many ways n queens can be placed on an n × n chessboard so that no two attack each other (same row, column or diagonal).

Example 1

Input: n = 4
Output: 2

Example 2

Input: n = 1
Output: 1

Constraints

  • 1 ≤ n ≤ 9

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

N-Queens (count solutions) — coding interview practice — Apexed