Problem
You get an integer n. Place n queens on an n x n chess board, so that no two queens attack each other.
A queen attacks every square in its row and in its column. It also attacks every square on its two diagonals.
Return all the different solutions. Show each solution as a list of n strings. String i shows row i of the board. In each string, Q marks a queen and . marks an empty square.
You can return the solutions in any order.
Example 1
- Input:
- n = 6
- Output:
- [[".Q....","...Q..",".....Q","Q.....","..Q...","....Q."],["..Q...",".....Q",".Q....","....Q.","Q.....","...Q.."],["...Q..","Q.....","....Q.",".Q....",".....Q","..Q..."],["....Q.","..Q...","Q.....",".....Q","...Q..",".Q...."]]
- Explanation:
- A 6 x 6 board has exactly 4 solutions.
Example 2
- Input:
- n = 3
- Output:
- []
- Explanation:
- On a 3 x 3 board, each placement of 3 queens has two queens that attack each other.
Constraints
- 1 <= n <= 9
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Beautiful Arrangement
Coding · Array · Backtracking · Bit Manipulation
- Maximum Path Quality of a Graph
Coding · Array · Backtracking · Graph Theory
- Optimal Account Balancing
Coding · Array · Backtracking · Bit Manipulation
- Subsets
Coding · Array · Backtracking · Bit Manipulation
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 2 records
Microsoft
Leetcode 51. N-Queens
Reported Sep 30, 2026 · 2 source reports
MID_LEVEL, PRINCIPAL
Source snapshot: Nov 12, 2026
View sourceNetapp
Leetcode 51. N-Queens
Reported Jun 4, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 1 record
Microsoft
N-Queens
Reported date not specified · Source window: 3mo
Source snapshot: Oct 20, 2026
Source frequency score: 56.3. This is not an interview probability.
View source