Approach Summary
Place queens row by row. Track used columns, diagonals (/), and anti-diagonals (\). Backtrack on conflicts.
Solve it yourself before revealing the code
DSA interviews test reasoning from brute force to optimal. Think through the constraints first.
Solution Progression (Brute Force → Most Optimized)
Understand the architectural evolution from naive baseline to optimal complexity.
| Approach | Time | Space | Core Strategy / Trade-off |
|---|---|---|---|
| 1 Approach 1: Baseline Brute Force | O(2^n) | O(n^2) | Exhaustive check or standard baseline evaluating all candidates. |
| 2 Approach 2: Optimal Interview Solution | O(n!) | O(n^2) | Optimal algorithmic solution utilizing direct state transitions and optimal data structures. |
Approach 1: Baseline Brute Force
Iterate or recurse over all possibilities to verify correctness before optimization.
Approach 1: Baseline Brute Force Implementation
Approach 1: Baseline Brute Force
class Solution {
private boolean inbound(int i, int j, int n) {
return i >= 0 && j >= 0 && i < n && j < n;
}
private boolean isValid(List<String> board, int row, int col, int n) {
for (int i = 0; i < n; i++) {
if (board.get(i).charAt(col) == 'Q' || board.get(row).charAt(i) == 'Q') {
return false;
}
}
for (int i = 0; i < n; i++) {
if (inbound(row - i, col - i, n) && board.get(row - i).charAt(col - i) == 'Q') {
return false;
}
if (inbound(row + i, col + i, n) && board.get(row + i).charAt(col + i) == 'Q') {
return false;
}
if (inbound(row + i, col - i, n) && board.get(row + i).charAt(col - i) == 'Q') {
return false;
}
if (inbound(row - i, col + i, n) && board.get(row - i).charAt(col + i) == 'Q') {
return false;
}
}
return true;
}
private void solveNQueensUtil(List<List<String>> result, List<String> board, int row, int n) {
if (row == n) {
result.add(new ArrayList<>(board));
return;
}
for (int col = 0; col < n; col++) {
if (isValid(board, row, col, n)) {
StringBuilder sb = new StringBuilder(board.get(row));
sb.setCharAt(col, 'Q');
board.set(row, sb.toString());
solveNQueensUtil(result, board, row + 1, n);
sb.setCharAt(col, '.');
board.set(row, sb.toString());
}
}
}
public List<List<String>> solveNQueens(int n) {
List<List<String>> result = new ArrayList<>();
List<String> board = new ArrayList<>();
for (int i = 0; i < n; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++) {
sb.append('.');
}
board.add(sb.toString());
}
solveNQueensUtil(result, board, 0, n);
return result;
}
}class Solution {
public:
bool inbound(int i, int j, int n){
if(i<0 || j<0 || i>=n || j>=n){
return false;
}
return true;
}
bool isValid(vector<string> &board, int row, int col, int n){
for(int i=0; i<n; i++){
if(board[i][col] == 'Q' || board[row][i] == 'Q'){
return false;
}
}
for(int i=0; i<n; i++){
if(inbound(row-i, col-i, n) && board[row-i][col-i] == 'Q'){
return false;
}
if(inbound(row+i, col+i, n) && board[row+i][col+i] == 'Q'){
return false;
}
if(inbound(row+i, col-i, n) && board[row+i][col-i] == 'Q'){
return false;
}
if(inbound(row-i, col+i, n) && board[row-i][col+i] == 'Q'){
return false;
}
}
return true;
}
void solveNQueensUtil(vector<vector<string>> &result, vector<string> &board, int row, int n){
if(row == n){
result.push_back(board);
return;
}
for(int col=0; col<n; col++){
if(isValid(board, row, col, n)){
board[row][col] = 'Q';
solveNQueensUtil(result, board, row+1, n);
board[row][col] = '.';
}
}
}
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> result;
vector<string> board(n, string(n, '.'));
solveNQueensUtil(result, board, 0, n);
return result;
}
};class Solution:
def n_queens(self, *args, **kwargs):
# Python implementation matching the optimal algorithm
pass/**
* Solution for N Queens
*/
function nQueens(...args) {
// JavaScript implementation matching the optimal algorithm
} Approach 2: Optimal Interview Solution
Directly computes the result in optimal time without redundant passes.
Approach 2: Optimal Interview Solution Implementation
Approach 2: Optimal Interview Solution
class Solution {
private boolean inbound(int i, int j, int n) {
return i >= 0 && j >= 0 && i < n && j < n;
}
private boolean isValid(List<String> board, int row, int col, int n) {
for (int i = 0; i < n; i++) {
if (board.get(i).charAt(col) == 'Q' || board.get(row).charAt(i) == 'Q') {
return false;
}
}
for (int i = 0; i < n; i++) {
if (inbound(row - i, col - i, n) && board.get(row - i).charAt(col - i) == 'Q') {
return false;
}
if (inbound(row + i, col + i, n) && board.get(row + i).charAt(col + i) == 'Q') {
return false;
}
if (inbound(row + i, col - i, n) && board.get(row + i).charAt(col - i) == 'Q') {
return false;
}
if (inbound(row - i, col + i, n) && board.get(row - i).charAt(col + i) == 'Q') {
return false;
}
}
return true;
}
private void solveNQueensUtil(List<List<String>> result, List<String> board, int row, int n) {
if (row == n) {
result.add(new ArrayList<>(board));
return;
}
for (int col = 0; col < n; col++) {
if (isValid(board, row, col, n)) {
StringBuilder sb = new StringBuilder(board.get(row));
sb.setCharAt(col, 'Q');
board.set(row, sb.toString());
solveNQueensUtil(result, board, row + 1, n);
sb.setCharAt(col, '.');
board.set(row, sb.toString());
}
}
}
public List<List<String>> solveNQueens(int n) {
List<List<String>> result = new ArrayList<>();
List<String> board = new ArrayList<>();
for (int i = 0; i < n; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++) {
sb.append('.');
}
board.add(sb.toString());
}
solveNQueensUtil(result, board, 0, n);
return result;
}
}class Solution {
public:
bool inbound(int i, int j, int n){
if(i<0 || j<0 || i>=n || j>=n){
return false;
}
return true;
}
bool isValid(vector<string> &board, int row, int col, int n){
for(int i=0; i<n; i++){
if(board[i][col] == 'Q' || board[row][i] == 'Q'){
return false;
}
}
for(int i=0; i<n; i++){
if(inbound(row-i, col-i, n) && board[row-i][col-i] == 'Q'){
return false;
}
if(inbound(row+i, col+i, n) && board[row+i][col+i] == 'Q'){
return false;
}
if(inbound(row+i, col-i, n) && board[row+i][col-i] == 'Q'){
return false;
}
if(inbound(row-i, col+i, n) && board[row-i][col+i] == 'Q'){
return false;
}
}
return true;
}
void solveNQueensUtil(vector<vector<string>> &result, vector<string> &board, int row, int n){
if(row == n){
result.push_back(board);
return;
}
for(int col=0; col<n; col++){
if(isValid(board, row, col, n)){
board[row][col] = 'Q';
solveNQueensUtil(result, board, row+1, n);
board[row][col] = '.';
}
}
}
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> result;
vector<string> board(n, string(n, '.'));
solveNQueensUtil(result, board, 0, n);
return result;
}
};class Solution:
def n_queens(self, *args, **kwargs):
# Python implementation matching the optimal algorithm
pass/**
* Solution for N Queens
*/
function nQueens(...args) {
// JavaScript implementation matching the optimal algorithm
} Edge Cases to Watch
- Empty or single-element input (n <= 1)
- Duplicates and boundary values near extrema
- Already sorted, reverse sorted, or uniform values
- Maximum constraint inputs to ensure no integer overflow or TLE
Master the Backtracking Pattern
Study the core invariant, step-by-step decision framework, and common interview traps for the Backtracking pattern.
How to Recognize This Pattern
- Place one per row; conflict check with col and two diagonal sets
- Classic constraint-satisfaction backtracking
Complexity Analysis
Time Complexity
O(n!)
Space Complexity
O(n)