Skip to main content

92. N-Queens II

hardAsked at Salesforce

Count the number of distinct N-queens placements without returning the actual boards. Salesforce uses this to test whether you can specialize an enumeration algorithm.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce uses count-only variants for capacity planning.
  • LeetCode Discuss (2025)Follow-up to N-Queens.

Problem

The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. Given an integer n, return the number of distinct solutions to the n-queens puzzle.

Constraints

  • 1 <= n <= 9

Examples

Example 1

Input
n = 4
Output
2

Example 2

Input
n = 1
Output
1

Approaches

1. Same as N-Queens but count only

Same backtracking, but instead of building the board, increment a counter at each complete placement.

Time
O(n!)
Space
O(n)
function totalNQueens(n) {
  const cols = new Set(), pos = new Set(), neg = new Set();
  let count = 0;
  function dfs(r) {
    if (r === n) { count++; return; }
    for (let c = 0; c < n; c++) {
      if (cols.has(c) || pos.has(r + c) || neg.has(r - c)) continue;
      cols.add(c); pos.add(r + c); neg.add(r - c);
      dfs(r + 1);
      cols.delete(c); pos.delete(r + c); neg.delete(r - c);
    }
  }
  dfs(0);
  return count;
}

Tradeoff: Simpler than LC 51 because we skip board construction.

2. Bitmask variant

Use three bitmasks (cols, pos, neg) for O(1) conflict check and ultra-fast bit ops.

Time
O(n!)
Space
O(n)
function totalNQueens(n) {
  let count = 0;
  function dfs(r, cols, pos, neg) {
    if (r === n) { count++; return; }
    let available = ((1 << n) - 1) & ~(cols | pos | neg);
    while (available) {
      const c = available & -available;
      available -= c;
      dfs(r + 1, cols | c, (pos | c) << 1, (neg | c) >> 1);
    }
  }
  dfs(0, 0, 0, 0);
  return count;
}

Tradeoff: Bitmask is dramatically faster constants — about 10x. Salesforce sometimes asks for it explicitly.

Salesforce-specific tips

Salesforce uses count-only enumeration for capacity planning (how many valid configurations exist?). They grade on whether you specialize from LC 51 cleanly — dropping the board allocation is the key insight. Bonus signal: introduce the bitmask version if asked for further optimization.

Common mistakes

  • Reusing the LC 51 solution verbatim — wastes work on board construction.
  • Off-by-one on the bit-shift in the bitmask version (pos shifts up, neg shifts down).
  • Not exploiting symmetry when n is even — halves the search.

Follow-up questions

An interviewer at Salesforce may pivot to one of these next:

  • N-Queens (LC 51) — return all boards.
  • Sudoku Solver (LC 37).
  • Counting permutations under arbitrary constraints.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why bitmask over sets?

Bitmask gives single-instruction conflict check and update. For n <= 9 (constraint), the constant factor improvement is dramatic.

What does available & -available do?

Isolates the LOWEST set bit. Standard trick for iterating set bits one at a time.

Practice these live with InterviewChamp.AI

Drill N-Queens II and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →