What is the best complexity of N-Queens puzzle?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The right answer depends on which version of the N-Queens problem you mean. The decision problem, the constructive problem, and the counting problem look similar on the surface, but their complexity is very different.
The First Important Distinction
People often ask for the complexity of "the N-Queens puzzle" as if there were only one task. In practice there are at least three common tasks:
- decide whether a solution exists for size
N - construct one valid arrangement
- count all valid arrangements
Those are not the same problem.
Decision Complexity Is Trivial
If the question is only "does an N x N board have a solution," there is a simple theorem:
- '
N = 1has a solution' - '
N = 2andN = 3do not' - every
N >= 4has at least one solution
So the decision problem can be answered in constant time once you know this result.
From a strict complexity point of view, that is O(1) decision time.
Constructing One Solution Can Be Done Efficiently
If the task is to produce one valid board, you do not need an exhaustive search for every N. There are constructive patterns that build a valid arrangement for all solvable sizes.
That means the "find one solution" version is much better than the usual backtracking presentation suggests. In principle, you can construct a solution in linear or near-linear time depending on the exact method and representation.
A simple educational approach is still backtracking because it is easy to understand:
This works well for moderate N, but its worst-case search behavior is still exponential.
Why Backtracking Is Usually Quoted As O(N!)
The classic search places one queen per row and tries unused columns. In the worst case, that resembles exploring permutations of columns, giving an upper bound around O(N!).
That is not a tight mathematical statement for every optimized implementation, but it is the standard practical description: exhaustive search grows exponentially and becomes expensive quickly.
Optimizations such as:
- diagonal conflict sets
- bit masks
- symmetry pruning
make the solver much faster in practice, but they do not turn exhaustive enumeration into a polynomial-time algorithm.
Counting All Solutions Is The Expensive Version
If the goal is to count every valid arrangement, the problem is fundamentally harder than just finding one.
A backtracking counter looks like this:
This is the version where exponential growth is unavoidable in any straightforward exact solver, because you must explore a huge search space to enumerate or count all valid boards.
So What Is The "Best Complexity"?
The best answer is:
- decision:
O(1)once the existence result is known - construct one solution: efficient constructive methods exist, much better than brute-force search
- count or enumerate all solutions: exponential-time search in practice
That is why a single number like O(N!) is incomplete. It describes the common backtracking solver, not every formulation of the problem.
Common Pitfalls
The most common mistake is quoting O(N!) without saying which problem is being solved. That is fine for naïve exhaustive search, but wrong for the decision version.
Another mistake is assuming "find one solution" and "count all solutions" have roughly the same complexity. They do not.
People also confuse asymptotic complexity with practical runtime. Bit-mask solvers can be dramatically faster than textbook backtracking even though the problem still has exponential search structure in the exhaustive case.
Finally, do not use small examples such as N = 8 to make claims about asymptotic behavior. The interesting difficulty appears as N grows.
Summary
- N-Queens has multiple problem formulations, and their complexities differ.
- Deciding existence is
O(1)because solutions exist forN = 1and allN >= 4. - Constructing one solution can be done efficiently with constructive methods.
- Exhaustively counting or enumerating solutions is exponential in practice.
- '
O(N!)is a useful description for classic backtracking, not a universal answer for every version of N-Queens.'

