← TikTok Interview Insights

TikTok·Data Scientist·Technical Phone Screen·Senior

Senior
Jul 2026

Summary

TikTok Data Scientist interview that went deep into classical AI search algorithms. More graph theory than I expected for a DS role, but looking back it makes sense given how much recommendation and ranking work they do.

Questions Asked (4)

Q1

Given a weighted graph with nodes S, A, B, C, D, G and specific edge weights, trace the node expansion order and final path found by BFS (treating edges as unweighted), uniform-cost search, and A* with the provided heuristic values. Break ties alphabetically.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I spent most of my mental energy.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the graph structure and heuristic values, then systematically trace each algorithm step-by-step, maintaining a priority queue or frontier and tracking visited nodes. Compare the expansion orders and final paths, highlighting how BFS ignores weights, uniform-cost search uses cumulative cost, and A* balances cost and heuristic.

Pro tip: Explicitly state your tie-breaking rule (alphabetical) and apply it consistently; also, note that A* is optimal only if the heuristic is admissible and consistent, and mention that in practice, you'd verify these properties.

1. Clarify the problem

Restate the graph, edge weights, heuristic values, and tie-breaking rule to ensure understanding and set the stage for tracing.

2. Trace BFS

Treat all edges as unweighted, use a FIFO queue, and expand nodes level by level, breaking ties alphabetically, to find the path with the fewest edges.

3. Trace Uniform-Cost Search

Use a priority queue ordered by cumulative path cost, expand the lowest-cost node, and break ties alphabetically, to find the minimum-weight path.

4. Trace A*

Use a priority queue ordered by f(n) = g(n) + h(n), expand the node with lowest f, break ties alphabetically, and stop when the goal is expanded.

5. Compare and conclude

Summarize the expansion orders and final paths, and discuss why they differ, emphasizing the role of weights and heuristics.

Key Points to Mention

  • BFS finds the path with the fewest edges, not necessarily the lowest cost, because it ignores weights.
  • Uniform-cost search is equivalent to Dijkstra's algorithm and guarantees the optimal path when all edge weights are non-negative.
  • A* uses a heuristic to guide search; it is optimal if the heuristic is admissible (never overestimates) and consistent (satisfies triangle inequality).
  • Tie-breaking rules (e.g., alphabetical) can affect the order of expansion but not the optimality of the final path if the algorithm is correct.
  • The choice of algorithm depends on the problem: BFS for unweighted graphs, uniform-cost for weighted graphs without a good heuristic, and A* for weighted graphs with a good heuristic.
  • In practice, A* often expands fewer nodes than uniform-cost search, but its performance depends on the heuristic's quality.

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

Q2

Prove whether the given heuristic h is admissible and consistent for the provided graph.

Algorithms & Data Structures
Author's notes

Admissibility I handled fine, just checked that h never exceeds the true cost to goal for each node.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clearly define admissibility and consistency for a heuristic in the context of A* search. Then, for the given graph, verify admissibility by checking that h(n) ≤ h*(n) for all nodes, and consistency by checking that h(n) ≤ c(n, n') + h(n') for all edges. Use the graph's structure and edge costs to systematically prove or disprove each property.

Pro tip: Emphasize that consistency implies admissibility, so if you can prove consistency, admissibility follows automatically. Also, mention that for graphs with negative edge costs, consistency may fail, but admissibility could still hold.

1. Define the properties

State the formal definitions of admissible and consistent heuristics. Admissible: h(n) ≤ h*(n) for all nodes n, where h*(n) is the true cost to the goal. Consistent: h(n) ≤ c(n, n') + h(n') for every edge (n, n'), and h(goal) = 0.

2. Analyze the graph

Identify all nodes, edges, and edge costs. Determine the true shortest path costs h*(n) from each node to the goal, either by inspection or by running a shortest path algorithm like Dijkstra's.

3. Check admissibility

For each node, compare the given heuristic value h(n) with the true cost h*(n). If h(n) ≤ h*(n) for all nodes, the heuristic is admissible; otherwise, provide a counterexample.

4. Check consistency

For each edge (n, n'), verify that h(n) ≤ c(n, n') + h(n'). If this holds for all edges and h(goal) = 0, the heuristic is consistent. If any edge violates this, the heuristic is inconsistent.

5. Conclude and relate

State whether the heuristic is admissible and/or consistent. If consistent, note that it is also admissible. If admissible but not consistent, explain the implications for A* search.

Key Points to Mention

  • Admissibility guarantees A* finds the optimal solution, while consistency ensures that nodes are expanded in non-decreasing order of f(n) and no node needs to be re-expanded.
  • Consistency implies admissibility, but the converse is not always true.
  • For graphs with negative edge costs, consistency may not hold, but admissibility might still be possible if h(n) never overestimates.
  • The heuristic must satisfy h(goal) = 0 for both admissibility and consistency.
  • When proving consistency, it's sufficient to check the triangle inequality for all edges.
  • If the heuristic is inconsistent, A* may still find the optimal solution if it re-expands nodes, but it may be less efficient.

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

Q3

Compare the time and space complexity of DFS, BFS, UCS, and A* in terms of branching factor b and depth d. Describe a scenario where DFS wins on memory but fails to give an optimal solution.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The complexity table part was straightforward.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining the complexities for each algorithm in terms of b and d, then compare them systematically. Use a concrete example to illustrate DFS's memory advantage and its failure to find optimal solutions. Finally, discuss the trade-offs and when each algorithm is appropriate.

Pro tip: Emphasize that DFS's memory advantage comes from not storing the frontier, but this often leads to revisiting nodes and missing optimal paths. Mention that in practice, DFS is rarely used for optimal search unless combined with other techniques like iterative deepening.

1. Define Time and Space Complexities

State the time and space complexity for each algorithm: DFS: O(b^m) time, O(bm) space (m is max depth); BFS: O(b^d) time, O(b^d) space; UCS: O(b^(1+floor(C*/ε))) time and space; A*: O(b^d) time and space in worst case, but often better with a good heuristic.

2. Compare Complexities

Highlight that BFS and UCS are memory-intensive as they store all frontier nodes, while DFS uses linear space. A*'s complexity depends on the heuristic; with a perfect heuristic, it's O(d) time and space.

3. Describe a Scenario Where DFS Wins on Memory

Consider a search tree with a large branching factor and deep solution, e.g., a puzzle with many possible moves. DFS only stores the current path, using O(bm) memory, while BFS would need O(b^d) memory, which is infeasible.

4. Explain Why DFS Fails to Give an Optimal Solution

DFS explores one branch fully before backtracking, so it may find a suboptimal solution if it goes down a deep path with a high-cost solution, while a shallower path with lower cost exists. It does not guarantee optimality unless all solutions are at the same depth.

5. Conclude with Trade-offs

Summarize that DFS is memory-efficient but not optimal or complete in infinite spaces. BFS is optimal for unweighted graphs but memory-heavy. UCS and A* are optimal for weighted graphs, with A* being more efficient with a good heuristic.

Key Points to Mention

  • Time complexity: DFS O(b^m), BFS O(b^d), UCS O(b^(1+floor(C*/ε))), A* O(b^d) worst case.
  • Space complexity: DFS O(bm), BFS O(b^d), UCS O(b^(1+floor(C*/ε))), A* O(b^d) worst case.
  • DFS uses linear space but may not find optimal solution; BFS is optimal for unweighted graphs but uses exponential space.
  • UCS is optimal for weighted graphs with non-negative costs; A* is optimal with admissible heuristic and often more efficient.
  • Scenario: DFS wins on memory in deep search trees with large branching factor, e.g., pathfinding in a large maze where memory is constrained.
  • DFS fails optimality when a suboptimal solution is found first due to depth-first exploration order.

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

Q4

Under what condition does A* reduce to Dijkstra's algorithm? Illustrate using the heuristic values provided.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Blanked for a moment.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by stating the condition: A* reduces to Dijkstra's algorithm when the heuristic function h(n) is zero for all nodes. Then, using the provided heuristic values, show that if all h(n)=0, the priority f(n)=g(n)+h(n) becomes g(n), which is exactly Dijkstra's priority. Conclude by noting that this means the heuristic provides no guidance, so A* explores nodes in order of increasing distance from the start, just like Dijkstra.

Pro tip: Mention that even a constant heuristic (e.g., h(n)=c for all n) also reduces A* to Dijkstra because the constant cancels out in the priority ordering, but the standard condition is h(n)=0. This shows depth and avoids a common misconception.

1. State the condition

Clearly state that A* reduces to Dijkstra's algorithm when the heuristic function h(n) is zero for all nodes.

2. Explain the priority function

Recall that A* uses f(n) = g(n) + h(n), while Dijkstra uses f(n) = g(n). When h(n)=0, the two are identical.

3. Apply to provided heuristic values

Examine the given heuristic values and confirm that they are all zero. If so, explicitly state that this satisfies the condition.

4. Discuss implications

Explain that with h(n)=0, the heuristic is admissible but uninformative, so A* explores nodes in order of increasing g(n), matching Dijkstra's behavior.

5. Conclude with trade-offs

Note that while A* with h(n)=0 is correct, it loses the efficiency gains of a good heuristic, making it equivalent to Dijkstra in performance.

Key Points to Mention

  • A* uses f(n) = g(n) + h(n); Dijkstra uses f(n) = g(n).
  • Condition: h(n) = 0 for all nodes n.
  • With h(n)=0, the heuristic is admissible but provides no guidance.
  • A* with h(n)=0 explores nodes in the same order as Dijkstra (by increasing g(n)).
  • A constant heuristic h(n)=c also reduces to Dijkstra because the constant does not affect the ordering.
  • Trade-off: losing heuristic guidance means losing potential speedup, so A* becomes as inefficient as Dijkstra.

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