Top K similar movies from a similarity graph
I was asked this
No reports yet. Sign in to say where you were asked it.
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 tokmovie ids in recommendation order. A movie is eligible if it is reachable fromstart, not inwatched, and is notstartitself. Order eligible movies by fewest hops fromstart, then higher rating, then smaller id. If fewer thankare eligible, return them all. Return[]whenk <= 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
requestsat leasthot_thresholdtimes. -
classify(requests, cache_entries, updated_movies, hot_threshold) -> list[str]— one"id:action"per movie, sorted by id, where the action isrecompute_and_protect(stale and hot),recompute,protect_hot_keyorok.
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.
adjmay contain repeated edges.watchedmay or may not containstart. - 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
startbe excluded even if it is not inwatched? - Can ratings tie, and what breaks the tie?
- Do we stop the search early once we have
kmovies?
What a strong answer covers
- BFS from
startgives 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 oncekare collected. O(V + E) plus sorting the layers you touch. - A set for
watched; never recommendstart. - Part 2: count requests once, put
updated_moviesin 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
dhops 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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
