Top K Videos by Multiplicative Similarity
Updated
Problem statement
A video catalog contains n videos labeled 0 through n - 1. Each row [u, v, w] of edges means videos u and v have direct similarity w in both directions.
The similarity of a path is the product of its edge weights. For each video reachable from source, use the maximum product over all paths from source to that video. Return up to k reachable video IDs other than source, ordered by decreasing maximum similarity. Break equal-score ties by smaller video ID. If fewer than k other videos are reachable, return all of them.
All graph edges are available locally for this coding exercise. A discussion follow-up asks how to fetch a node that is not stored locally; an RPC-backed neighbor lookup is not a graded input.
Function
topKSimilarVideos(n: int, edges: double[][], source: int, k: int) → int[]Examples
Example 1
n = 5edges = [[0,1,0.5],[1,2,0.5],[0,3,0.25],[3,4,1.0]]source = 0k = 3return = [1, 2, 3]Video 1 scores 0.5. Videos 2, 3, and 4 each score 0.25, so the smaller IDs 2 and 3 fill the remaining slots.
Example 2
n = 3edges = [[0,1,0.25],[0,2,0.5],[2,1,1.0]]source = 0k = 2return = [1, 2]The indirect route to video 1 scores 0.5, improving its direct 0.25 link. It ties video 2 and wins by ID.
Example 3
n = 4edges = [[0,1,0.5]]source = 0k = 3return = [1]Only video 1 is reachable from video 0, so the result contains fewer than three IDs.
Example 4
n = 2edges = [[0,1,1.0]]source = 0k = 0return = []Requesting zero recommendations returns an empty array.
Constraints
2 <= n <= 200;0 <= edges.length <= n * (n - 1) / 2.- Each edge has two distinct valid video IDs, and no unordered pair occurs twice.
- Each edge weight is exactly
0.25,0.5, or1.0. 0 <= source < 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
- Maximum K-Star SumAkuna Capital · OAAkuna Capital interview questions
- Mutual-Friend Referral RecommendationsFireworks AI · PHONE SCREEN · ONSITE INTERVIEW
- Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- Minimum Railway PlatformsMakeMyTrip.com · ONSITE INTERVIEWMakeMyTrip.com interview questions
- Reduce Array Size to At Most HalfMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Top-Down Employee Reporting ForestOkta · ONSITE INTERVIEWOkta interview questions
- Maximize Monsters DefeatedSalesforce · ONSITE INTERVIEWSalesforce interview questions