← TikTok Interview Insights

TikTok·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

TikTok data engineer interview with a classic backtracking problem. Not much context given but the N-Queens question was the whole show, so I'm guessing this was a technical phone screen or OA-style round.

Questions Asked (1)

Q1

Print all possible solutions to the N-Queens problem.

Algorithms & Data Structures
Author's notes

I knew the problem but froze a bit on the 'print all solutions' part because I'd only ever coded the 'count solutions' version before.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use backtracking to place queens row by row, ensuring no two queens attack each other by checking columns and diagonals. At each row, try all columns, and if safe, place a queen and recurse; when a solution is found, record it. Prune invalid placements early to reduce search space.

Pro tip: Mention that you can optimize safety checks using sets or boolean arrays for columns and diagonals, reducing time complexity from O(N!) to O(N!) with lower constant factors. Also, note that for N=1 there is one solution, and for N=2,3 there are none, showing awareness of edge cases.

1. Clarify and Define

Confirm the problem: place N queens on an N×N board so that no two attack each other. Clarify output format (e.g., list of board configurations) and constraints (e.g., N ≤ 9 for typical interviews).

2. Choose Backtracking

Explain that backtracking is ideal because it incrementally builds candidates and abandons a candidate as soon as it determines it cannot lead to a valid solution.

3. Design State and Safety Checks

Represent the board with a 1D array where index is row and value is column. Use sets or boolean arrays to track occupied columns, diagonals (row+col), and anti-diagonals (row-col) for O(1) safety checks.

4. Implement Recursive Search

Write a recursive function that places a queen in the current row, iterating over all columns. If safe, mark the column and diagonals, recurse to the next row, then unmark (backtrack). When row equals N, record the solution.

5. Analyze Complexity and Edge Cases

Discuss time complexity O(N!) and space O(N). Mention edge cases: N=1 (one solution), N=2,3 (no solutions), and that the number of solutions grows rapidly.

Key Points to Mention

  • Backtracking algorithm with pruning
  • Using 1D array to represent queen positions
  • O(1) safety checks with sets/boolean arrays for columns and diagonals
  • Time complexity O(N!) and space O(N)
  • Edge cases: N=1, N=2, N=3
  • Output format: list of board configurations (e.g., strings with 'Q' and '.')

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.