Top-Rated Similar Movies
Updated
Problem statement
There are n movies with IDs 0 through n - 1. Movie i has rating ratings[i] and a list similar[i] of movie IDs. Given a starting movie start and an integer k, return the IDs of the highest-rated eligible movies.
Recommendation rules
- The lists describe a directed graph: an entry
vinsimilar[u]allows traversal fromutov. It does not imply a reverse edge. - A movie is eligible if it is reachable from
startby following one or more listed edges, including paths through other movies. The starting movie itself is always excluded, even if a cycle reaches it again. - Lists may contain duplicate entries and self-edges. Each eligible movie appears at most once in the answer.
- Rank eligible movies by rating in descending order. When ratings tie, the smaller movie ID comes first.
- Return the first
min(k, number of eligible movies)IDs in that order. Return an empty array whenk = 0or no movie is eligible.
Ratings belong to movies. Edges have no numerical scores, and path length does not affect ranking. A high-rated movie may be reachable only through low-rated movies, so those intermediate movies still need to be explored.
Function
topRatedSimilarMovies(ratings: int[], similar: int[][], start: int, k: int) → int[]Examples
Example 1
ratings = [60,80,95,95,100]similar = [[1,2],[3],[],[],[]]start = 0k = 2return = [2,3]Movies 1, 2, 3 are reachable. Movies 2 and 3 tie at rating 95, so their IDs appear in ascending order. Movie 4 is unreachable despite its higher rating.
Example 2
ratings = [100,20,90,100]similar = [[1],[0,2],[],[]]start = 0k = 4return = [2,1]Only movies 1 and 2 are eligible. The cycle back to movie 0 does not make the starting movie a recommendation, and movie 3 is unreachable.
Example 3
ratings = [7,9]similar = [[1],[0]]start = 0k = 0return = []No recommendations are requested when k = 0.
Constraints
1 <= n <= 1500andratings.length = similar.length = n.0 <= ratings[i] <= 10^9.- The total number of entries across all adjacency lists is at most
3000, counting duplicates and self-edges. - Every listed movie ID is in
[0, n - 1]. 0 <= start < nand0 <= k <= n.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Aggregate Machine TopologyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Deterministic Depth-First Graph TraversalWaymo · PHONE SCREENWaymo interview questions
- Traverse an Up-Down-Right Pointer StructureBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Reconstruct ItineraryTripjack · ONSITE INTERVIEWTripjack interview questions
- Module Rebuild CostsAirbnb · OAAirbnb interview questions
- Priority Graph: First Arrival TimesIntuit · OAIntuit interview questions
- Evolving Merchant Clusters and Persistent PinsStripe · OAStripe interview questions
- Create Lexicographically Largest PermutationOracle · OAOracle interview questions
- Topological OrderingApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions