Place Non-Attacking Queens
Company: Bytedance
Role: Site Reliability Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's competency in combinatorial search, constraint reasoning, and algorithmic design for arranging non-attacking pieces on a grid.
Constraints
- 1 <= n <= 9
- Each configuration must place exactly one queen in every row
- No two queens may share a column, main diagonal, or anti-diagonal
Examples
Input: 1
Expected Output: [["Q"]]
Explanation: On a 1x1 board, the only valid configuration is to place the queen in the single available cell.
Input: 2
Expected Output: []
Explanation: There is no way to place 2 queens on a 2x2 board without them attacking each other.
Input: 3
Expected Output: []
Explanation: There is no valid arrangement for 3 queens on a 3x3 board.
Input: 4
Expected Output: [[".Q..", "...Q", "Q...", "..Q."], ["..Q.", "Q...", "...Q", ".Q.."]]
Explanation: These are the two valid 4x4 boards produced by placing queens row by row from left to right.
Hints
- Try placing queens row by row. Once you decide a row, you only need to check whether a column or diagonal is already occupied.
- A cell (row, col) belongs to main diagonal (row - col) and anti-diagonal (row + col). These values can be tracked in sets for O(1) conflict checks.