Count the Ways to Place n Non-Attacking Queens on an n x n Board

Quick Overview

Count how many ways n queens can be placed on an n by n chessboard so that no two share a row, a column or a diagonal, with rotations and reflections counted as different placements. It tests exhaustive search over placements and exact handling of the attack rules.

Count the Ways to Place n Non-Attacking Queens on an n x n Board

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Place `n` queens on an `n x n` chessboard so that no two queens attack each other. A queen attacks every square in its row, in its column and on both of its diagonals, so in a valid placement no two queens share a row, a column or a diagonal. Given `n`, return the number of distinct valid placements. ### Function Signature ```python def count_queen_placements(n: int) -> int: ``` ### Rules - Every placement uses exactly `n` queens. - Two placements are distinct when their sets of occupied squares differ. Placements that are rotations or reflections of each other are counted separately. ### Constraints - `1 <= n <= 10` - The answer is below `1000` for every allowed `n`. ### Examples #### Example 1 ```text Input: n = 4 Output: 2 ``` Listing the queen's column for rows 0 to 3, the two placements are `[1, 3, 0, 2]` and `[2, 0, 3, 1]`. #### Example 2 ```text Input: n = 1 Output: 1 ``` A single queen on a single square is a valid placement. #### Example 3 ```text Input: n = 3 Output: 0 ``` Every arrangement of three queens in different rows and columns leaves two of them on a shared diagonal.

Overview: Count how many ways n queens can be placed on an n by n chessboard so that no two share a row, a column or a diagonal, with rotations and reflections counted as different placements. It tests exhaustive search over placements and exact handling of the attack rules.

ByteDance logo
ByteDance
Nov 8, 2026
hardSoftware EngineerTechnical ScreenCoding & Algorithms
1
0

Place n queens on an n x n chessboard so that no two queens attack each other. A queen attacks every square in its row, in its column and on both of its diagonals, so in a valid placement no two queens share a row, a column or a diagonal.

Given n, return the number of distinct valid placements.

Function Signature

def count_queen_placements(n: int) -> int:

Rules

  • Every placement uses exactly n queens.
  • Two placements are distinct when their sets of occupied squares differ. Placements that are rotations or reflections of each other are counted separately.

Constraints

  • 1 <= n <= 10
  • The answer is below 1000 for every allowed n .

Examples

Example 1

Input:  n = 4
Output: 2

Listing the queen's column for rows 0 to 3, the two placements are [1, 3, 0, 2] and [2, 0, 3, 1].

Example 2

Input:  n = 1
Output: 1

A single queen on a single square is a valid placement.

Example 3

Input:  n = 3
Output: 0

Every arrangement of three queens in different rows and columns leaves two of them on a shared diagonal.