Skip to content
Decoder

N-Queens (return the boards)

Hard

Problem

Place n queens on an n by n chessboard so that no two queens attack each other (no shared row, column or diagonal). Return every distinct board, in any order. Each board is a list of n strings, one per row, with "Q" for a queen and "." for an empty square.

Example 1

Input
n = 4
Output
[[".Q..",

Example 2

            "...Q",

Example 3

            "Q...",

Example 4

            "..Q."],

Example 5

           ["..Q.",

Example 6

            "Q...",

Example 7

            "...Q",

Example 8

            ".Q.."]]

Example 9

Input
n = 1
Output
[["Q"]]

Constraints

  • 1 <= n <= 9

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.