Recommend videos watched by friends at a given friendship distance, ordered by frequency and then name.
Given a list of each person's watched videos and an undirected friendship graph, find the videos watched by people who are exactly friendship steps away from a given person.
Collect all videos watched by those friends, count how many times each video appears, and return the videos sorted by:
- increasing frequency,
- lexicographically increasing video name for ties.
This is a graph traversal and aggregation problem that combines distance-limited search with frequency-based sorting.
Input Format
- An array
watchedVideos, wherewatchedVideos[i]is the list of videos watched by personi. - An adjacency list
friends, wherefriends[i]contains the direct friends of personi. - An integer
idfor the starting person. - An integer
kfor the friendship distance.
Output Format
- Return a list of video titles watched by people whose shortest friendship distance from
idis exactlyk. - The result must be sorted by frequency ascending, then by title ascending.
Constraints
- The friendship graph is undirected.
0 <= id < nk >= 0- Video titles are compared lexicographically using standard string ordering.
- The exact original platform constraints are not assumed here; solve under typical interview-scale limits.
Example 1
Input
watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, k = 2
Output
["D"]
Explanation
At distance 2 from person 0 is person 3. Person 3 watched only video D, so the answer is ["D"].
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.