Solving N-Queens Problem... How far can we go?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The N-Queens problem asks how to place N queens on an N x N board so no two queens attack each other. It is a classic search problem used to teach pruning, symmetry reduction, and bit-level optimization. The interesting question is not only how to solve it, but how far practical solvers can scale under different goals.
Baseline Backtracking Approach
A standard solver places one queen per row and checks whether a candidate column is safe from previous placements.
This is clear and correct, but set-based checks become a bottleneck as N grows.
Bitmask Optimization
Bitmasks replace sets with integer operations, which are significantly faster in tight recursion loops.
For counting problems, bitmasks are usually the biggest single performance improvement.
Symmetry Reduction
The board is symmetric around the vertical axis. You can reduce top-level work by exploring only half of first-row placements and mirroring counts.
For odd N, the center column must be handled separately.
Symmetry reduction plus bitmasks often gives major speedups for counting mode.
What "How Far" Means
Scalability depends on task definition:
- Find one valid board.
- Count all solutions.
- Enumerate all board layouts.
Finding one solution scales much farther than full enumeration. Counting all solutions is expensive but practical for moderate to high N in optimized implementations.
Language matters too. Python can go far with bitmasks, but high-end records are usually achieved with compiled languages and aggressive low-level tuning.
Parallel Search Strategy
N-Queens branches naturally. You can split by first-row or first-two-row placements and process branches in parallel.
Parallelism helps, but pruning quality remains the primary factor.
Verification and Correctness Checks
Optimization introduces risk. Always validate against known small-board counts:
- '
N=4has2solutions.' - '
N=8has92solutions.'
Use these values in automated tests before trusting performance benchmarks.
Practical Engineering Lessons
N-Queens teaches techniques that transfer to real systems:
- Constraint propagation.
- Search-space pruning.
- Symmetry-aware decomposition.
- Representation tradeoffs between readability and speed.
These patterns appear in scheduling, placement, and other combinatorial optimization problems.
Common Pitfalls
- Benchmarking enumeration and counting as if they were the same workload.
- Keeping full board matrices in recursion when only column and diagonal state is needed.
- Implementing symmetry reduction incorrectly for odd board sizes.
- Reporting performance without correctness checks on known counts.
- Assuming parallelism can compensate for weak pruning.
Summary
- Backtracking solves N-Queens conceptually, while bitmasks make it fast in practice.
- Symmetry reduction significantly reduces top-level search work.
- Practical limits depend on whether you seek one solution, counts, or full listings.
- Parallelization helps after pruning and representation are already optimized.
- Always verify optimized solvers against known small
Ncounts before scaling up.
Related reading
- Solving Range Minimum Queries using Binary Indexed Trees Fenwick Trees
- Solving string reduction challenge
- Solving The 8 Puzzle With A Algorithm
- Some followup questions about consistent hashing
- Sort a vector in which the n first elements have been already sorted?
- Sort Algorithm - find which chart bar sees different bar
- Sort 2 lists in Python based on the ratio of individual corresponding elements or based on a third list
- Sort a 2d array by a column value

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.