1311. Get Watched Videos by Your Friends LeetCode Solution
In this guide, you will get 1311. Get Watched Videos by Your Friends LeetCode Solution with the best time and space complexity. The solution to Get Watched Videos by Your Friends problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Get Watched Videos by Your Friends solution in C++
- Get Watched Videos by Your Friends solution in Java
- Get Watched Videos by Your Friends solution in Python
- Additional Resources

Problem Statement of Get Watched Videos by Your Friends
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.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i
Complexity Analysis
- Time Complexity:
- Space Complexity:
1311. Get Watched Videos by Your Friends LeetCode Solution in C++
class Solution {
public:
vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos,
vector<vector<int>>& friends, int id,
int level) {
vector<string> ans;
queue<int> q{{id}};
vector<bool> seen(friends.size());
seen[id] = true;
unordered_map<string, int> count;
set<pair<int, string>> freqAndVideo;
for (int i = 0; i < level; ++i)
for (int sz = q.size(); sz > 0; --sz) {
for (const int friend_ : friends[q.front()])
if (!seen[friend_]) {
seen[friend_] = true;
q.push(friend_);
}
q.pop();
}
for (int i = q.size(); i > 0; --i) {
for (const string& video : watchedVideos[q.front()])
++count;
q.pop();
}
for (const auto& : count)
freqAndVideo.insert({freq, video});
for (const auto& [_, video] : freqAndVideo)
ans.push_back(video);
return ans;
}
};
/* code provided by PROGIEZ */
1311. Get Watched Videos by Your Friends LeetCode Solution in Java
class Solution {
public List<String> watchedVideosByFriends(List<List<String>> watchedVideos, int[][] friends,
int id, int level) {
Queue<Integer> q = new ArrayDeque<>(List.of(id));
boolean[] seen = new boolean[friends.length];
seen[id] = true;
Map<String, Integer> count = new HashMap<>();
for (int i = 0; i < level; ++i)
for (int sz = q.size(); sz > 0; --sz) {
for (final int friend : friends[q.peek()])
if (!seen[friend]) {
seen[friend] = true;
q.offer(friend);
}
q.poll();
}
for (final int friend : q)
for (final String video : watchedVideos.get(friend))
count.merge(video, 1, Integer::sum);
List<String> ans = new ArrayList<>(count.keySet());
ans.sort((a, b)
-> count.get(a).equals(count.get(b)) ? a.compareTo(b)
: count.get(a).compareTo(count.get(b)));
return ans;
}
}
// code provided by PROGIEZ
1311. Get Watched Videos by Your Friends LeetCode Solution in Python
class Solution:
def watchedVideosByFriends(
self,
watchedVideos: list[list[str]],
friends: list[list[int]],
id: int,
level: int,
) -> list[str]:
seen = [False] * 100
seen[id] = True
q = collections.deque([id])
count = collections.Counter()
for _ in range(level):
for _ in range(len(q)):
curr = q.popleft()
for friend in friends[curr]:
if not seen[friend]:
seen[friend] = True
q.append(friend)
for friend in q:
for video in watchedVideos[friend]:
count += 1
return sorted(count, key=lambda video: (count, video))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.