N-Queens Problem Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓N-Queens Problem runs in O(n!) time and O(n) space, in Go as in every other language.
- ✓The math/bits package is the real answer: bits.OnesCount32, bits.TrailingZeros32 and bits.LeadingZeros32 give you the hardware-backed popcount/bit-scan the language otherwise has no operator for.
- ✓Go has a dedicated AND-NOT ('bit clear') operator, &^, so 'positions not in cols|diag1|diag2' is one operator, full &^ (cols|diag1|diag2), instead of the two-operator & ~(...) every other C-family language here needs, a real piece of Go-only syntax.
- ✓The Go trap: Go defines integer overflow completely, in the spec, with no debug/release distinction: unsigned arithmetic (including <<) always wraps modulo 2^width, silently, in every build.
- ✓bits.TrailingZeros32 and bits.OnesCount32 both compile to a single BSF/TZCNT or POPCNT instruction on amd64 when the compiler recognizes the pattern (they are compiler intrinsics despite being ordinary exported functions).
What N-Queens Problem actually is
N-Queens Problem in Go: what you actually reach for
Go implementation
package queens
import "math/bits"
func Solve(n uint) [][]int {
var solutions [][]int
full := uint32(1)<<n - 1
board := make([]int, n)
var place func(row uint, cols, diag1, diag2 uint32)
place = func(row uint, cols, diag1, diag2 uint32) {
if row == n {
cp := make([]int, n)
copy(cp, board)
solutions = append(solutions, cp)
return
}
available := full &^ (cols | diag1 | diag2) // Go's AND-NOT operator
for available != 0 {
pos := available & -available // unsigned unary minus is legal Go
available -= pos
board[row] = bits.TrailingZeros32(pos)
place(row+1, cols|pos, (diag1|pos)<<1, (diag2|pos)>>1)
}
}
place(0, 0, 0, 0)
return solutions
}
// count := len(Solve(8)) // 92Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(n!)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for N-Queens Problem?
The math/bits package is the real answer: bits.OnesCount32, bits.TrailingZeros32 and bits.LeadingZeros32 give you the hardware-backed popcount/bit-scan the language otherwise has no operator for. There is no bitset type in the standard library, so the board state is just three plain uint32 values.
What is the most common Go mistake when implementing N-Queens Problem?
Go defines integer overflow completely, in the spec, with no debug/release distinction: unsigned arithmetic (including <<) always wraps modulo 2^width, silently, in every build. uint32(1) << n for a variable n >= 32 just wraps to 0 at runtime with no panic and no warning, the exact bug Rust's debug build would have caught for you. You have to bound-check n yourself before trusting the mask.
How does N-Queens Problem actually perform in Go?
bits.TrailingZeros32 and bits.OnesCount32 both compile to a single BSF/TZCNT or POPCNT instruction on amd64 when the compiler recognizes the pattern (they are compiler intrinsics despite being ordinary exported functions). Go's escape analysis keeps row/cols/diag1/diag2 on the stack across the recursive calls as long as nothing about them escapes, so the hot search loop allocates nothing until a finished board is copied into the result slice. Time complexity is O(n!) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what Devin offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.