PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/TikTok

Solve the N-Queens problem

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's competence in backtracking, constraint satisfaction, combinatorial search, recursion, and state representation. It is commonly asked in technical interviews because it probes algorithmic problem-solving and handling of combinatorial explosion within the Coding & Algorithms domain, testing both conceptual understanding and practical implementation skills.

  • easy
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Solve the N-Queens problem

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

## Problem Given an integer **n**, place **n** queens on an **n × n** chessboard so that **no two queens attack each other** (i.e., no two queens share the same row, column, or diagonal). Return **all distinct valid boards**. ### Input - An integer `n` (typically `1 ≤ n ≤ 9` in interviews). ### Output - A list of solutions, where each solution is an array of `n` strings of length `n`. - Each string contains only: - `'Q'` for a queen - `'.'` for an empty cell ### Example For `n = 4`, one valid output format is: - `[".Q..","...Q","Q...","..Q."]` (Your output may list solutions in any order.) ### Notes - You may assume there is **at most one queen per row** (or per column) in your construction approach.

Quick Answer: This question evaluates a candidate's competence in backtracking, constraint satisfaction, combinatorial search, recursion, and state representation. It is commonly asked in technical interviews because it probes algorithmic problem-solving and handling of combinatorial explosion within the Coding & Algorithms domain, testing both conceptual understanding and practical implementation skills.

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
Feb 11, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
2
0
Loading...

Problem

Given an integer n, place n queens on an n × n chessboard so that no two queens attack each other (i.e., no two queens share the same row, column, or diagonal).

Return all distinct valid boards.

Input

  • An integer n (typically 1 ≤ n ≤ 9 in interviews).

Output

  • A list of solutions, where each solution is an array of n strings of length n .
  • Each string contains only:
    • 'Q' for a queen
    • '.' for an empty cell

Example

For n = 4, one valid output format is:

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

(Your output may list solutions in any order.)

Notes

  • You may assume there is at most one queen per row (or per column) in your construction approach.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More TikTok•More Software Engineer•TikTok Software Engineer•TikTok Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,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.