Count N-Queens Placements
Updated
Problem statement
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) → intExamples
Example 1
n = 5return = 10There 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 = 0Wherever 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 = 4There 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