← TikTok Interview Insights

TikTok·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

TikTok software engineer coding round, two algorithmic questions back to back. Both were fairly involved for a single session and the graph one in particular had a lot of moving parts to cover.

Questions Asked (2)

Q1

Given a directed graph with n vertices and m edges, return a valid topological ordering if one exists. If the graph has a cycle, detect it and explain how your approach finds it. Implement both a BFS in-degree approach and a DFS-based approach, compare their time and space complexity, and discuss how you'd handle multiple valid orderings.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This was a lot to ask in one question.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then implement Kahn's algorithm (BFS in-degree) and DFS-based topological sort, explaining how each detects cycles. Compare their time and space complexities, and discuss handling multiple valid orderings by noting that any valid order is acceptable unless a specific tie-breaking rule is required.

Pro tip: Mention that Kahn's algorithm can detect cycles by checking if the number of processed nodes is less than n, while DFS detects cycles via back edges. Also, note that both approaches have O(n+m) time complexity, but DFS may use more space due to recursion stack.

1. Clarify requirements and edge cases

Confirm whether the graph is guaranteed to be a DAG, if multiple orderings are acceptable, and discuss edge cases like disconnected graphs, self-loops, and empty graphs.

2. Implement Kahn's algorithm (BFS in-degree)

Compute in-degrees, enqueue nodes with zero in-degree, and process them while decrementing in-degrees of neighbors. If the processed count is less than n, a cycle exists.

3. Implement DFS-based topological sort

Perform DFS, marking nodes as unvisited, visiting, or visited. Add nodes to the front of a list upon completion. A back edge to a visiting node indicates a cycle.

4. Compare time and space complexity

Both run in O(n+m) time. Kahn's uses O(n) space for in-degree array and queue; DFS uses O(n) for visited states and recursion stack, which can be O(n) in worst case.

5. Discuss multiple valid orderings

Explain that any valid topological order is acceptable unless a specific order is required. To handle multiple orderings, one can use a priority queue (e.g., min-heap) to always pick the smallest available node, producing a lexicographically smallest order.

Key Points to Mention

  • Kahn's algorithm detects cycles by checking if the number of processed nodes equals n.
  • DFS detects cycles via back edges (node in visiting state).
  • Both algorithms have O(n+m) time complexity; space complexity is O(n) for both, but DFS may use O(n) recursion stack.
  • Multiple valid topological orderings exist for graphs with multiple sources; any is acceptable unless tie-breaking is specified.
  • To produce a deterministic order, use a priority queue (e.g., min-heap) in Kahn's algorithm.
  • Edge cases: disconnected graphs, self-loops, and graphs with no edges.

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

Q2

Given a string text and a shorter string pattern (both lowercase), return all starting indices in text where a substring is a permutation of pattern. Solve it in O(n) time using a sliding window with character frequency counts, and handle edge cases like repeated characters, an empty pattern, or a pattern longer than the text.

Algorithms & Data Structures
Author's notes

Sliding window with a fixed-size frequency array, pretty standard anagram-finding problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window of length equal to the pattern, maintaining character frequency counts for the window and the pattern. Compare the frequency maps at each step to find anagrams, updating counts as the window slides. Handle edge cases like empty pattern, pattern longer than text, and repeated characters by checking lengths upfront and using efficient data structures.

Pro tip: Emphasize that the frequency comparison can be optimized to O(1) per window by tracking the number of matching characters instead of comparing entire maps, and mention that this approach is optimal for large inputs.

1. Clarify edge cases and constraints

Confirm that both strings are lowercase, discuss handling of empty pattern (return all indices or empty list?), pattern longer than text (return empty list), and repeated characters. This shows thoroughness.

2. Initialize frequency counts

Create frequency arrays (size 26) for the pattern and for the first window of text. Compute the initial number of matches between the two arrays.

3. Slide the window and update counts

Iterate through the text, adding the new character and removing the old character from the window frequency array, updating the match count accordingly. If matches equal 26, record the start index.

4. Return the result

Collect all starting indices where the window is a permutation of the pattern and return them.

Key Points to Mention

  • Time complexity O(n) where n is the length of text, and space complexity O(1) since frequency arrays are fixed size (26).
  • Sliding window technique with fixed window size equal to pattern length.
  • Using frequency arrays (or hash maps) to compare character counts efficiently.
  • Optimization: track the number of matching characters to avoid O(26) comparison per window.
  • Edge cases: empty pattern (return all indices or empty list?), pattern longer than text (return empty list), and repeated characters.
  • Handling of lowercase letters only, allowing use of fixed-size arrays.

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