Place N Non-Attacking Queens and Return Every Board

Last updated: November 29, 2026

Quick Overview

Return every arrangement of n queens on an n by n board such that no two queens share a row, column or diagonal, with each board shown as a list of strings.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Return every arrangement of n queens on an n by n board such that no two queens share a row, column or diagonal, with each board shown as a list of strings.

N-Queens remains a staple Meta onsite backtracking question because it shows whether you can encode constraints for O(1) checks and keep state symmetric between choose and unchoose. Strong candidates also discuss bitmask versions and symmetry pruning.

What the Interviewer Expects
  • Place one queen per row to eliminate row conflicts
  • Track columns, diagonals (r - c) and anti-diagonals (r + c) in sets
  • Check conflicts in O(1)
  • Undo all state on return
  • Know the solution counts for small n as sanity checks
Key Topics to Cover
Backtracking
Constraint satisfaction
Hash set
Bit manipulation
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
  • How would you count solutions for n = 14 fast using bitmasks?
  • How would you use board symmetry to halve the work?
  • How would you return just one solution in O(n) without search?
  • How would you extend this to pieces that also move like knights?
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
  • n from 1 to 9.
  • Return all distinct boards, any order.
  • Q marks a queen, . an empty square....
High-Level Design

Fill rows top to bottom. For row r, try each column c that is not attacked: column c is unused, diagonal r - c is unused, and anti-diagonal r + c is unused. Mark all three, record the column...


Submit Your Answer
Markdown supported