There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:

Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1 Output: ["B","C"] Explanation: You have id = 0 (green color in the figure) and your friends are (yellow color in the figure): Person with id = 1 -> watchedVideos = ["C"] Person with id = 2 -> watchedVideos = ["B","C"] The frequencies of watchedVideos by your friends are: B -> 1 C -> 2
Example 2:

Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2 Output: ["D"] Explanation: You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length2 <= n <= 1001 <= watchedVideos[i].length <= 1001 <= watchedVideos[i][j].length <= 80 <= friends[i].length < n0 <= friends[i][j] < n0 <= id < n1 <= level < nfriends[i] contains j, then friends[j] contains iProblem Overview: You are given a social network where each person has a list of watched videos and a list of friends. Starting from a specific person id, find all friends exactly level connections away, collect the videos they watched, and return them sorted by frequency and then lexicographically.
Approach 1: Breadth-First Search (BFS) on Graph (O(n + m + k log k) time, O(n + k) space)
Treat the friendship list as an adjacency list of an unweighted graph. Run Breadth-First Search starting from the given id. BFS naturally explores nodes level by level, so you stop expanding once you reach the required distance. Use a queue to process users and a visited set to avoid revisiting nodes. After reaching the target level, iterate through those friends and aggregate their watched videos in a frequency map (hash table).
Once the counts are collected, convert the map into a list of video titles and sort it. The sorting rule is key: first by frequency (ascending), then by video name (lexicographically). This is typically implemented using a custom comparator or sorting key. BFS ensures you only traverse each friend once, giving O(n + m) traversal time where n is users and m is friendship edges. The final sorting step dominates when many videos are collected, giving O(k log k) where k is the number of distinct videos.
Approach 2: Depth-First Search (DFS) with Level Tracking (O(n + m + k log k) time, O(n + k) space)
An alternative is a recursive DFS that tracks the current depth from the starting user. The recursion explores neighbors until the depth equals the target level. At that point, instead of continuing deeper, collect the watched videos of that friend. A visited array or set prevents infinite loops in cyclic friendships.
DFS works because you explicitly track depth in the recursive calls. However, it tends to explore deeper paths earlier and may visit unnecessary branches before reaching the correct level. You still use a hash table (hash table) to count video frequencies and apply the same sorting rule at the end. While the asymptotic complexity matches BFS, the control flow is less intuitive for level-based queries.
Recommended for interviews: BFS is the expected solution. The problem explicitly asks for friends at a specific distance in a graph, which is exactly what BFS is designed for. Implementing BFS with a queue, visited set, frequency map, and final sort demonstrates solid understanding of graph traversal and practical data structure usage.
This approach leverages BFS to traverse the friends network layer by layer starting from the given person ID. We only consider the friends at the exact level specified and grab their watched videos to determine the output.
The above Python function first initializes a queue and a visited set to perform BFS starting from the given 'id'. We iterate level by level until the desired 'level' is reached, updating the queue with each friend's friends. Once the desired level is reached, we tally the frequency of each video watched by those on that level and sort them first by frequency, then by alphabetical order.
Python
JavaScript
Time Complexity: O(n + v log v) where 'n' is the number of nodes and 'v' is the number of videos.
Space Complexity: O(n + v) where 'v' is for storing video frequencies.
This approach uses DFS to traverse the social network to find friends at the given level. It uses recursion to explore each friend's connections until reaching the desired depth. Then, collects and sorts videos watched by the friends at that level.
This C++ function uses recursive DFS to find friends exactly at the specified level away from the starting ID. It maintains a visited set to handle cycles and uses an unordered map to count video frequencies. Finally, it sorts results by frequency and name.
Time Complexity: O(n^2 + v log v) where 'n' is the number of people and 'v' is the number of different videos.
Space Complexity: O(n + v) to store visited nodes and video frequency counts.
We can use the Breadth-First Search (BFS) method to start from id and find all friends at a distance of level, then count the videos watched by these friends.
Specifically, we can use a queue q to store the friends at the current level. Initially, add id to the queue q. Use a hash table or a boolean array vis to record the friends that have already been visited. Then, perform level iterations, in each iteration dequeue all friends from the queue and enqueue their friends until all friends at distance level are found.
Next, we use a hash table cnt to count the videos watched by these friends and their frequencies. Finally, sort the key-value pairs in the hash table in ascending order by frequency, and if frequencies are the same, sort by video name in ascending order. Return the sorted list of video names.
Time complexity is O(n + m + v times log v), and space complexity is O(n + v). Here, n and m are the lengths of the arrays watchedVideos and friends, respectively, and v is the total number of videos watched by all friends.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Breadth-First Search (BFS) | Time Complexity: O(n + v log v) where 'n' is the number of nodes and 'v' is the number of videos. |
| Depth-First Search (DFS) | Time Complexity: O(n^2 + v log v) where 'n' is the number of people and 'v' is the number of different videos. |
| BFS | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Breadth-First Search (BFS) | O(n + m + k log k) | O(n + k) | Best for level-based traversal in graphs; naturally finds nodes at exact distance |
| Depth-First Search (DFS) with Level Tracking | O(n + m + k log k) | O(n + k) | Useful when recursion is preferred or when exploring graph paths with explicit depth control |
Get Watched Videos by Your Friends | Graphs | Leetcode | DSA | Hindi • ShashCode • 2,497 views views
Watch 7 more video solutions →Practice Get Watched Videos by Your Friends with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor