← TikTok Interview Insights

TikTok·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

TikTok software engineering interview with three algorithm problems back to back. Nothing too exotic but the text justification one was more annoying to implement cleanly than I expected.

Questions Asked (3)

Q1

Given an array of words and a max line width, format the words into fully justified text where each line is exactly that width. Spaces should be distributed as evenly as possible, with extra spaces going to the leftmost gaps. The last line is left-justified.

Algorithms & Data Structures
Author's notes

This one bit me.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem constraints and edge cases, then outline a greedy line-packing algorithm that determines how many words fit per line. For each line except the last, distribute spaces evenly with extra spaces on the left; for the last line, left-justify with single spaces. Finally, discuss time/space complexity and potential optimizations.

Pro tip: Mention that you would handle edge cases like a single word exceeding the max width or empty input, and that you'd test with examples to ensure correct space distribution. This shows attention to detail and robustness.

1. Clarify requirements and edge cases

Ask about input constraints (e.g., word length vs. maxWidth, empty array) and confirm the justification rules, especially for the last line and extra spaces.

2. Design greedy line-packing algorithm

Iterate through words, accumulating characters and spaces to determine the maximum number of words that fit in each line without exceeding maxWidth.

3. Distribute spaces for full justification

For each line except the last, calculate total spaces needed, then distribute evenly with extra spaces assigned to the leftmost gaps. For the last line, join words with a single space and pad with trailing spaces.

4. Implement and test with examples

Write clean code, then walk through test cases like ["This", "is", "an", "example", "of", "text", "justification."] with maxWidth=16 to verify correctness.

5. Analyze complexity and discuss optimizations

State that the algorithm runs in O(n) time where n is total characters, and O(1) extra space excluding output. Mention potential optimizations like precomputing word lengths.

Key Points to Mention

  • Greedy approach to pack as many words as possible per line.
  • Handling of the last line differently: left-justified with single spaces.
  • Even distribution of spaces with extra spaces on the leftmost gaps.
  • Edge cases: single word longer than maxWidth, empty input, multiple spaces between words.
  • Time and space complexity analysis.
  • Testing with provided examples and additional edge cases.

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

Q2

Given a string of '(', ')', and '*' characters where '*' can represent either parenthesis or an empty string, determine if there's some assignment of the wildcards that produces a valid balanced sequence. Optionally return one valid assignment.

Algorithms & Data Structures
Author's notes

Greedy approach works here.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a greedy approach with a range of possible open parenthesis counts, updating the range as you process each character. At the end, check if 0 is within the range; if so, the string can be balanced. To return a valid assignment, do a second pass using a stack or backtracking to assign '*' characters appropriately.

Pro tip: Discuss both the greedy range method for feasibility and a backtracking method for constructing an assignment, showing you understand trade-offs between time and space. Mention that the greedy method runs in O(n) time and O(1) space, which is optimal.

1. Clarify the problem

Confirm that '*' can be '(', ')', or empty, and that we need to determine if any assignment yields a balanced sequence. Ask if returning one valid assignment is required or just a boolean.

2. Greedy range approach for feasibility

Maintain a range [low, high] of possible open parenthesis counts. For '(', increment both; for ')', decrement both (clamp low to 0); for '*', decrement low (clamp to 0) and increment high. At the end, check if low == 0.

3. Construct a valid assignment (if needed)

If a valid assignment is required, use a stack to track indices of '(' and '*'. First pass: match ')' with '(' or '*'. Second pass: match remaining '(' with '*' to the right. Replace unmatched '*' with empty string.

4. Analyze complexity

State that the greedy feasibility check runs in O(n) time and O(1) space. The construction method also runs in O(n) time but uses O(n) space for the stack.

5. Test with examples

Walk through examples like '(*)' (valid), '(*))' (invalid), and '**' (valid) to demonstrate correctness and edge cases.

Key Points to Mention

  • Greedy range tracking: low and high bounds for open parentheses
  • Handling of '*' as both open and close, and empty
  • Clamping low to 0 to avoid negative counts
  • Final condition: low == 0 indicates a valid assignment exists
  • Two-pass stack-based construction for returning an assignment
  • Time and space complexity: O(n) time, O(1) space for feasibility, O(n) space for construction

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

Q3

In a 2D grid of open cells and obstacles, find the shortest path between two given coordinates using only up/down/left/right movement. Return the path as a list of coordinates, or an empty list if none exists. Also handle out-of-bounds or blocked start/end cells.

Algorithms & Data Structures
Author's notes

Standard BFS.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use BFS to find the shortest path in an unweighted grid, tracking parent pointers to reconstruct the path. First validate inputs (start/end in bounds and not blocked), then run BFS from start to end, and finally backtrack from end to start to build the path list.

Pro tip: Mention that BFS guarantees the shortest path in unweighted grids, and proactively discuss edge cases like start == end, no path, and invalid inputs. Also, note that using a visited set or modifying the grid in-place can optimize space.

1. Input Validation

Check if start and end coordinates are within grid bounds and not obstacles. If invalid, return an empty list immediately.

2. BFS Initialization

Initialize a queue with the start cell, a visited set to avoid revisiting, and a parent map (or 2D array) to track the path.

3. BFS Traversal

While the queue is not empty, dequeue a cell, check if it's the end, and if not, enqueue all valid unvisited neighbors (up, down, left, right).

4. Path Reconstruction

If the end is reached, backtrack from end to start using the parent map to construct the path, then reverse it to get start-to-end order.

5. Return Result

If BFS completes without reaching the end, return an empty list. Otherwise, return the reconstructed path.

Key Points to Mention

  • BFS is optimal for unweighted shortest path problems.
  • Use a queue for level-order traversal and a visited set to avoid cycles.
  • Track parent pointers to reconstruct the path efficiently.
  • Handle edge cases: start == end, start or end out of bounds, start or end blocked.
  • Time complexity: O(R*C) where R and C are grid dimensions; space complexity: O(R*C) for visited and parent structures.
  • Consider in-place modification of the grid to mark visited cells if allowed, to save space.

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