This practice workspace is built for desktop. Open it on a larger screen to start solving.
N-Queens
Place n queens on an n x n chessboard such that no two queens attack each other (no two queens share the same row, column, or diagonal).
Return all distinct solutions. Each solution is represented as a list of n strings of length n, where 'Q' marks a queen and '.' marks an empty cell.
Use backtracking: place one queen per row, tracking which columns and diagonals are already occupied, backtrack when no valid column exists.
For deterministic comparison, sort the outer list of boards lexicographically (compare element-by-element across the string arrays).
Examples
Constraints
- 1 <= n <= 9
Preview Mode
This is a read-only preview of DSAMind's AI coaching. Sign up to get custom feedback on your own solution.
Your approach
- Pattern:Backtracking
- Time:O(N)
- Space:O(1)
Complete approach — pattern and complexity both named.
Coach
What you got right
You correctly matched the sorted input requirement and implemented a linear-time scan using two pointers.
Where it diverged
No divergence detected. The code correctly aligns with the stated approach.
Next attempt: focus on
Try solving related sliding window problems to build familiarity with two-pointer variants.
Signals
No signals fired — clean run, you stayed in flow.