N-Queens - Problem
The N-Queens Problem is a classic chess puzzle that challenges you to place n queens on an n ร— n chessboard such that no two queens can attack each other.

In chess, a queen can attack any piece in the same row, column, or diagonal. Your task is to find all possible arrangements where n queens coexist peacefully on the board.

Input: An integer n representing the size of the chessboard
Output: A list of all distinct solutions, where each solution is represented as a list of strings. Each string represents a row of the chessboard, with 'Q' indicating a queen and '.' indicating an empty space.

For example, with n = 4, one valid solution would place queens at positions that don't threaten each other, creating a safe configuration for all pieces.

Input & Output

example_1.py โ€” Python
$ Input: n = 4
โ€บ Output: [[".Q..","...Q","Q...","..Q."], ["..Q.","Q...","...Q",".Q.."]]
๐Ÿ’ก Note: For a 4ร—4 board, there are exactly 2 distinct solutions. In the first solution, queens are placed at (0,1), (1,3), (2,0), (3,2). In the second solution, queens are at (0,2), (1,0), (2,3), (3,1). Both arrangements ensure no two queens can attack each other.
example_2.py โ€” Python
$ Input: n = 1
โ€บ Output: [["Q"]]
๐Ÿ’ก Note: For a 1ร—1 board, there's only one solution: place the single queen in the only available position. This is the trivial base case.
example_3.py โ€” Python
$ Input: n = 2
โ€บ Output: []
๐Ÿ’ก Note: For a 2ร—2 board, it's impossible to place 2 queens without them attacking each other. Any placement will result in queens sharing the same row, column, or diagonal, so the result is an empty list.

Visualization

Tap to expand
โ™›โ™›โ™›โ™›Valid 4-Queens Solutionโœ… Row 0: Queen at column 1โœ… Row 1: Queen at column 3โœ… Row 2: Queen at column 0โœ… Row 3: Queen at column 2Algorithm steps:1. Place queen in row 02. Try each column in row 13. Backtrack if no valid position4. Continue until solution found
Understanding the Visualization
1
Initialize Board
Start with empty 4ร—4 board and tracking arrays for columns and diagonals
2
Place First Queen
Place queen in row 0, try each column until finding valid position
3
Recursive Placement
Move to next row, place queen avoiding conflicts with previous queens
4
Backtrack on Conflict
When no valid position exists, remove last queen and try next position
5
Solution Found
When all rows filled successfully, record the solution and continue search
Key Takeaway
๐ŸŽฏ Key Insight: Backtracking with constraint tracking reduces the search space from n! permutations to a much smaller tree by eliminating invalid partial solutions early.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(N!)

In the worst case, we might need to try N positions in first row, N-2 in second, etc., leading to roughly N! combinations, but with heavy pruning

n
2n
โœ“ Linear Growth
Space Complexity
O(N)

Recursion stack depth is N, plus arrays to track column and diagonal usage

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 9
  • Each solution contains a distinct board configuration
  • Solutions can be returned in any order
  • Board representation uses 'Q' for queens and '.' for empty spaces
Asked in
Google 85 Amazon 72 Meta 68 Microsoft 61
68.3K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen