Count Ways to Finish a Partially Placed N-Queens Board

Last updated: November 29, 2026

Quick Overview

Some queens are already fixed on an n by n board, at most one per row. Count the number of ways to place the remaining queens so that every row has exactly one queen and no two queens attack each other.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Some queens are already fixed on an n by n board, at most one per row. Count the number of ways to place the remaining queens so that every row has exactly one queen and no two queens attack each other.

A Google onsite twist on N-Queens that stops candidates from reciting the textbook version. The interviewer checks that you fold pre-placed pieces into the column and diagonal sets before searching, and that you validate the fixed queens themselves.

What the Interviewer Expects
  • Places one queen per row and tracks columns and both diagonals in sets
  • Validates the pre-placed queens against each other before searching
  • Skips rows that are already fixed and checks them only for consistency
  • Uses O(1) conflict checks
  • Discusses bitmask optimization for larger n
Key Topics to Cover
Backtracking
N-Queens
Constraint propagation
Bitmasks
Diagonal indexing
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Rewrite the search with bitmasks for columns and diagonals.
  • How would you return one completed board instead of a count?
  • How does the count change if the board wraps around (toroidal queens)?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: n (up to about 12) and a list fixed of (row, col) pairs, at most one per row.
  • Output: the number of complete placements with one queen per row, consistent with the fixed queens, and no ...
High-Level Design

Index diagonals so conflicts are O(1): cells on the same "" diagonal share r - c, cells on the same "/" diagonal share r + c. Keep three sets: cols, diag1, anti.

  1. Insert each fixed queen...

Submit Your Answer
Markdown supported