Dsa
Recursion
Easy
N-Queens Solver
The N-Queens puzzle is the problem of placing N chess queens on an N×N chessboard so that no two queens threaten each other. Given an integer N, return all distinct solutions to the N-Queens puzzle in a list of lists. Each sub-list represents one solution, where 'Q' denotes a queen and '.' denotes an empty space.
Input Format:
- A single integer N.
Output Format:
- Print each solution as a list of strings, each string representing a row of the chessboard.
Example:
Input:
4
Output:
['.Q..', '...Q', 'Q...', '..Q.']
['..Q.', 'Q...', '...Q', '.Q..']
Key concepts
recursionbacktrackingcombinatorics
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now