Watch 8 video solutions for Get Watched Videos by Your Friends, a medium level problem involving Array, Hash Table, Breadth-First Search. This walkthrough by ShashCode has 2,497 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |