Back to Practice
#0300
N-Queens Count
HardDSA40 min25 XP
Problem
Return the number of valid ways to place n queens on an n x n board so no two queens share a row, column, or diagonal.
Why This Matters
N-Queens is the classic backtracking constraint problem with columns and diagonals as state.
Function Signature
def total_n_queens(n):
Examples
Example 1
Inputn = 4
Output2
A 4x4 board has exactly two valid N-Queens arrangements.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Four queens
Input4
Expected2
The classic 4x4 case has two solutions.
One queen
Input1
Expected1
A single queen can be placed in one way.
Hidden Test Categories
n = 2n = 3n = 5