FastPrepCount N-Queens Placements
Problems/Count N-Queens Placements

Count N-Queens Placements

ByteDance logoByteDance● HardFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported as the coding question of a technical phone screen for a full-time software engineering role at TikTok USDS, which the candidate had applied to online. The report names the known N-Queens problem in its counting form: given n, return how many ways n queens fit on a board of n rows and n columns with no queen attacking another. The candidate rated it hard, had little backtracking practice and did not pass the round.

The report gives no limits and no examples. We chose a function that takes n and returns the count, and we kept the limits of the known problem, n from 1 to 9. As in the known problem, placements that are mirror images or rotations of each other count as different ways. The examples are ours.

Our version is about 95% the same as the reported question.

Given an integer n, count the ways to put n queens on a chessboard with n rows and n columns so that no queen can attack another, and return that count.

A queen attacks every square in its own row, in its own column and on both of its diagonals, at any distance. Two ways are different when some square holds a queen in one of them and is empty in the other, so ways that are mirror images or rotations of each other are counted separately. If there is no way, return 0.

Function

totalNQueens(n: int) → int

Examples

Example 1

n = 5return = 10

There are 10 ways. One of them puts the queens of rows 0 to 4 in columns 0, 2, 4, 1 and 3.

Example 2

n = 3return = 0

Wherever the queen of row 0 goes, at most one square of row 1 is safe, and after that no square of row 2 is safe. So no placement exists.

Example 3

n = 6return = 4

There are 4 ways. One puts the queens of rows 0 to 5 in columns 1, 3, 5, 0, 2 and 4. Its mirror image, columns 4, 2, 0, 5, 3 and 1, is another one.

Constraints

  • 1 <= n <= 9

More ByteDance Phone Screen questions

  • Matrix EscapePHONE SCREEN · Seen Nov 2026
  • Largest Number Below N from Reusable DigitsPHONE SCREEN · Seen Nov 2026
  • Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
  • Majority ElementPHONE SCREEN · Seen Nov 2026
  • Valid ParenthesesPHONE SCREEN · Seen Nov 2026
  • Binary Tree Target-Sum PathsPHONE SCREEN · Seen Nov 2026
  • Longest Increasing SubsequencePHONE SCREEN · Seen Nov 2026
  • Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximum Number of Events That Can Be AttendedPHONE SCREEN · Seen Nov 2026

Similar questions at other companies

  • Expression Add OperatorsPinterest · PHONE SCREENPinterest interview questions
  • All Safe Boards for N QueensAdobe · PHONE SCREENAdobe interview questions
  • Count Complete Tic-Tac-Toe GamesDecagon · PHONE SCREEN · ONSITE INTERVIEWDecagon interview questions
  • DFS Subsequences of a String (For MLE)CoinDCX · PHONE SCREEN
  • Permutations of Unique CharactersSalesforce · PHONE SCREENSalesforce interview questions
  • All Subsets with a Given SumApple · PHONE SCREENApple interview questions
  • Validate a Mahjong Hand PartitionDuolingo · PHONE SCREENDuolingo interview questions
  • Generate Fixed-Size CombinationsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Phone Keypad Letter CombinationsGoogle · ONSITE INTERVIEWGoogle interview questions
  • DFS Subsequences of a String (For MLE)Wayfair · OAWayfair interview questions

ByteDance interview experiences

  • ByteDance TikTok USDS SWE Intern Interview: 1 Experience
  • ByteDance MLE New Grad Interviews: 1 Experience
  • ByteDance Security SRE Intern Phone Screens: 1 Experience
  • ByteDance USDS New Grad SWE Phone Screen: 1 Experience
  • ByteDance New Grad Data Engineer Video Interviews: 1 Experience
  • ByteDance Trust & Safety Research Scientist Intern: 1 Experience
All ByteDance interview questionsSee ByteDance hiring insights
Saved
class Solution {
    public int totalNQueens(int n) {
        // Write your code here.
    }
}
Checking account…