By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"The N-Queens problem isn’t just a chess puzzle—it’s a litmus test for backtracking mastery. FAANG interviewers use it to assess recursion depth, pruning efficiency, and your ability to optimize brute-force solutions. Nail this, and you prove you can handle combinatorial problems under pressure."
Before tackling N-Queens, ensure you understand: 1. Backtracking (DFS with pruning) – How to explore all possibilities while discarding invalid paths early. 2. Hash Sets / Arrays for Tracking State – Efficiently checking constraints (e.g., column, diagonal conflicts). 3. Recursion & Base Cases – How to structure recursive calls and termination conditions.
col
row-col
row+col
cols |= (1 << col)
Repeat this checklist for every N-Queens problem:
N
N x N
Output: All valid configurations (or count, if follow-up).
Choose a Representation
queens[row] = col
Option 3: Bitmask (fastest for large N).
Define Constraints
No two queens share the same diagonal (row1 - col1 == row2 - col2 or row1 + col1 == row2 + col2).
row1 - col1 == row2 - col2
row1 + col1 == row2 + col2
Backtracking Setup
row = 0
If a placement is valid, recurse to the next row.
Prune Invalid Paths Early
Before placing a queen at (row, col), check:
(row, col)
Base Case
If row == N, add the current configuration to the result.
row == N
Optimize (Optional)
Skip symmetric configurations (e.g., only generate solutions where the first queen is in the left half).
Edge Cases
N = 1
N = 2 or 3
N >= 4
Problem: Return all distinct solutions for N=4.
N=4
queens
def solveNQueens(n): def backtrack(row, cols, diag1, diag2, path, res): if row == n: res.append(path) return for col in range(n): curr_diag1 = row - col curr_diag2 = row + col if col not in cols and curr_diag1 not in diag1 and curr_diag2 not in diag2: backtrack( row + 1, cols | {col}, diag1 | {curr_diag1}, diag2 | {curr_diag2}, path + [col], res ) res = [] backtrack(0, set(), set(), set(), [], res) # Convert to board format boards = [] for sol in res: board = [] for col in sol: board.append("." col + "Q" + "." (n - col - 1)) boards.append(board) return boards
O(N!)
O(N)
Problem: Return the number of distinct solutions for N=8.
N=8
def totalNQueens(n): def backtrack(row, cols, diag1, diag2): if row == n: return 1 count = 0 for col in range(n): curr_diag1 = row - col curr_diag2 = row + col if (cols & (1 << col)) or (diag1 & (1 << curr_diag1)) or (diag2 & (1 << curr_diag2)): continue count += backtrack( row + 1, cols | (1 << col), diag1 | (1 << curr_diag1), diag2 | (1 << curr_diag2) ) return count return backtrack(0, 0, 0, 0)
Problem: Count solutions for N=12 without considering symmetric duplicates (e.g., rotations/reflections).
N=12
col < (N+1)/2
def totalNQueensSymmetry(n): def backtrack(row, cols, diag1, diag2): if row == n: return 1 count = 0 for col in range(n): if row == 0 and col > (n - 1) // 2: # Skip symmetric duplicates continue curr_diag1 = row - col curr_diag2 = row + col if (cols & (1 << col)) or (diag1 & (1 << curr_diag1)) or (diag2 & (1 << curr_diag2)): continue count += backtrack( row + 1, cols | (1 << col), diag1 | (1 << curr_diag1), diag2 | (1 << curr_diag2) ) return count total = backtrack(0, 0, 0, 0) if n % 2 == 0: return total 2 else: # For odd N, count center column separately center = (n - 1) // 2 total_center = backtrack(0, 1 << center, 1 << (-center), 1 << center) return (total - total_center) 2 + total_center
O(N! / 2)
if row == N: return
N=1
N=2
N=2/3
N=20
"Here’s your N-Queens cheat sheet: 1. Represent the board as a 1D array or bitmask. 2. Backtrack row-by-row, pruning invalid columns/diagonals early. 3. Use sets or bitmasking for O(1) conflict checks. 4. Optimize with symmetry if counting solutions. 5. Edge cases: N=1 (1 solution), N=2/3 (0 solutions).
Remember: Interviewers care about your pruning logic, not just the final answer. Walk them through your constraints, and you’ll stand out. Now go solve it!
This framework is directly usable in interviews—memorize the steps, and you’ll handle any N-Queens problem with confidence. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.