← System Design Simulator

N-Queens Visualization for Coding Interviews

By Rahul Kumar · Senior Software Engineer · Updated · Category: Greedy & Backtracking

Backtracking with row/diagonal constraint sets; solution enumeration.

Concept

The N-Queens problem asks you to place N queens on an N by N chessboard so that no two queens attack each other — no two share a row, column, or diagonal. It is the canonical backtracking problem used to teach constraint satisfaction, search tree pruning, and recursion. The solution count grows roughly like n! / exp(n) and is unknown in closed form; for n = 8 there are 92 solutions, for n = 20 there are 39 billion. Efficient solvers rely on three sets or bitmasks to track attacked columns and diagonals, enabling O(1) conflict checks per placement. N-Queens also demonstrates symmetry exploitation — reflecting along the vertical axis halves the work — and shows why pure DFS with pruning is the right tool for enumeration problems over structured combinatorial spaces.

N-Queens — Interactive Simulator

Runs fully client-side in your browser; no sign-up. Or open full screen →

Launch the interactive N-Queens visualization — animated algorithm, step-through, and live data-structure updates.

How it works

Place queens one row at a time, so the row constraint is satisfied by construction. For each row, try every column: if placing a queen at (row, col) does not conflict with any already-placed queen, recurse to the next row; otherwise skip. Conflict detection uses three sets: cols tracks used columns, diag1 tracks the 'top-left to bottom-right' diagonals (indexed by row - col, which is constant along such a diagonal), and diag2 tracks 'top-right to bottom-left' diagonals (indexed by row + col). When placing at (row, col), check that col is not in cols, row - col not in diag1, and row + col not in diag2. If clear, add all three markers, recurse, and remove them when backtracking (the restoration step is why backtracking is called backtracking). When row equals n, you have a full solution — record the board layout. A significantly faster variant uses three integers as bitmasks and finds legal placements via bit tricks: at each level, the bitmask of available columns is ~(cols | diag1 | diag2) & mask, and you iterate over its set bits. This drops the enumeration constant so that n = 15 finishes in a fraction of a second.

Implementation

NQueens.java — enumerate all solutions
import java.util.*;

public final class NQueens {
    private NQueens() {}

    public static List<List<String>> solveNQueens(int n) {
        List<List<String>> results = new ArrayList<>();
        int[] queens = new int[n];
        Set<Integer> cols = new HashSet<>();
        Set<Integer> diag1 = new HashSet<>();
        Set<Integer> diag2 = new HashSet<>();
        backtrack(0, n, queens, cols, diag1, diag2, results);
        return results;
    }

    private static void backtrack(int row, int n, int[] queens,
                                  Set<Integer> cols, Set<Integer> diag1, Set<Integer> diag2,
                                  List<List<String>> results) {
        if (row == n) {
            results.add(toBoard(queens, n));
            return;
        }
        for (int col = 0; col < n; col++) {
            int d1 = row - col, d2 = row + col;
            if (cols.contains(col) || diag1.contains(d1) || diag2.contains(d2)) continue;
            queens[row] = col;
            cols.add(col); diag1.add(d1); diag2.add(d2);
            backtrack(row + 1, n, queens, cols, diag1, diag2, results);
            cols.remove(col); diag1.remove(d1); diag2.remove(d2);
        }
    }

    private static List<String> toBoard(int[] queens, int n) {
        List<String> board = new ArrayList<>(n);
        for (int r = 0; r < n; r++) {
            char[] row = new char[n];
            Arrays.fill(row, '.');
            row[queens[r]] = 'Q';
            board.add(new String(row));
        }
        return board;
    }

    public static int countSolutions(int n) {
        int[] count = new int[1];
        countHelper(0, 0, 0, 0, n, (1 << n) - 1, count);
        return count[0];
    }

    private static void countHelper(int row, int cols, int d1, int d2, int n, int mask, int[] count) {
        if (row == n) { count[0]++; return; }
        int available = mask & ~(cols | d1 | d2);
        while (available != 0) {
            int bit = available & -available;
            available ^= bit;
            countHelper(row + 1, cols | bit, (d1 | bit) << 1, (d2 | bit) >> 1, n, mask, count);
        }
    }
}

Complexity

Common pitfalls

Key design decisions & trade-offs

Practice problems

Interview follow-ups

Why it matters

N-Queens is a core part of the Greedy & Backtracking toolkit. If you are preparing for a technical interview at a top-tier company or teaching a course, a working visualization is the fastest path from "I read about it" to "I understand it".

Related