PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/TikTok

Generate all safe queen placements on board

Last updated: Mar 29, 2026

Quick Overview

This question evaluates algorithm design for constraint-satisfaction problems, testing skills in search, recursion, backtracking, combinatorics, and state-space pruning.

  • hard
  • TikTok
  • Coding & Algorithms
  • Machine Learning Engineer

Generate all safe queen placements on board

Company: TikTok

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given an integer `n` representing the size of a chessboard (`n × n`). You need to place `n` queens on the board so that no two queens attack each other. A queen can attack another piece if they are on the **same row**, **same column**, or **same diagonal**. Return **all distinct valid configurations** of the board. - Each configuration should be represented as an array (or list) of `n` strings. - Each string has length `n` and represents one row of the board. - Character `'Q'` represents a queen, and `'.'` represents an empty square. Two configurations are considered different if they differ in at least one row string. You may assume: - `1 ≤ n ≤ 9`. **Example** Input: ```text n = 4 ``` One valid output (order of configurations and rows does not matter): ```text [ [".Q..", "...Q", "Q...", "..Q."], ["..Q.", "Q...", "...Q", ".Q.."] ] ``` In each configuration above, there are 4 queens on the board, and no two queens attack each other.

Quick Answer: This question evaluates algorithm design for constraint-satisfaction problems, testing skills in search, recursion, backtracking, combinatorics, and state-space pruning.

Related Interview Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)
TikTok logo
TikTok
Dec 8, 2025, 7:48 PM
Machine Learning Engineer
Technical Screen
Coding & Algorithms
2
0

You are given an integer n representing the size of a chessboard (n × n). You need to place n queens on the board so that no two queens attack each other.

A queen can attack another piece if they are on the same row, same column, or same diagonal.

Return all distinct valid configurations of the board.

  • Each configuration should be represented as an array (or list) of n strings.
  • Each string has length n and represents one row of the board.
  • Character 'Q' represents a queen, and '.' represents an empty square.

Two configurations are considered different if they differ in at least one row string.

You may assume:

  • 1 ≤ n ≤ 9 .

Example

Input:

n = 4

One valid output (order of configurations and rows does not matter):

[
  [".Q..",
   "...Q",
   "Q...",
   "..Q."],

  ["..Q.",
   "Q...",
   "...Q",
   ".Q.."]
]

In each configuration above, there are 4 queens on the board, and no two queens attack each other.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More TikTok•More Machine Learning Engineer•TikTok Machine Learning Engineer•TikTok Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.