← Questions

Top K similar movies from a similarity graph

0
GoogleCodingLast asked Dec 2025

A coding interview question asked in Google's onsite coding round, last asked Dec 2025. It tests graphs, bfs and heaps.

Movies form an undirected similarity graph: adj[i] lists the movies directly similar to movie i, and each movie has a rating. A user liked start. Recommend up to k movies they have not watched, closest in the graph first.

Part 1: recommend

  • recommend(adj, rating, start, watched, k) -> list[int] — return up to k movie ids in recommendation order. A movie is eligible if it is reachable from start, not in watched, and is not start itself. Order eligible movies by fewest hops from start, then higher rating, then smaller id. If fewer than k are eligible, return them all. Return [] when k <= 0.
adj     = [[1, 2], [0, 3], [0, 3, 4], [1, 2], [2]]
rating  = [5, 7, 6, 9, 8]
recommend(adj, rating, 0, [0, 2], 3)  ->  [1, 3, 4]
# 2 is watched; 1 is one hop away; 3 and 4 are two hops away and 3 rates higher

adj     = [[1, 2], [0, 3], [0, 4], [1], [2]]
rating  = [1, 8, 8, 7, 9]
recommend(adj, rating, 0, [0], 4)     ->  [1, 2, 4, 3]
# 1 and 2 tie on distance and rating, so the smaller id goes first

Part 2: the cache in production

Results are cached per starting movie. Each entry lists the dependency movies it was computed from. Classify every movie that appears in requests or as a key of cache_entries:

  • stale: one of its dependencies is in updated_movies (no entry or an empty list is never stale);

  • hot: it appears in requests at least hot_threshold times.

  • classify(requests, cache_entries, updated_movies, hot_threshold) -> list[str] — one "id:action" per movie, sorted by id, where the action is recompute_and_protect (stale and hot), recompute, protect_hot_key or ok.

classify([1, 2, 1, 3, 1, 2], {1: [1, 4, 5], 2: [2, 7], 4: [8]}, [5, 7], 3)
  ->  ["1:recompute_and_protect", "2:recompute", "3:ok", "4:ok"]

Constraints

  • Up to 10^5 movies. adj may contain repeated edges. watched may or may not contain start.
  • Part 2: up to 2 · 10^5 requests and dependency ids in total.

Clarifying questions

  • Is distance the number of hops, and is the graph connected?
  • Should start be excluded even if it is not in watched?
  • Can ratings tie, and what breaks the tie?
  • Do we stop the search early once we have k movies?

What a strong answer covers

  • BFS from start gives hop distance; mark nodes visited on enqueue so repeated edges are harmless.
  • Process one BFS layer at a time and sort only that layer by (-rating, id); stop once k are collected. O(V + E) plus sorting the layers you touch.
  • A set for watched; never recommend start.
  • Part 2: count requests once, put updated_movies in a set, take the union of keys, sort.
  • Production issues: stale caches after rating changes, hot keys and thundering herds (request coalescing, per-key throttling), huge graphs that need a depth limit.

Follow-ups

  • Limit the search to d hops so a popular hub doesn't make the BFS touch the whole catalogue.
  • Edges have similarity weights. How does the ranking change?
  • How would you invalidate only the cache entries that depend on a movie whose rating changed?

Where it's asked

  • Google · Onsite coding · Last asked Dec 202545 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)

What it tests

graphs · bfs · heaps

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding