Get Watched Videos by Your Friends - Problem
Imagine a social network where everyone watches videos and has friends. You want to discover new content by exploring videos watched by people at different "friendship levels" from you.

Level 1 friends: Your direct friends
Level 2 friends: Friends of your friends
Level k friends: People exactly k friendship connections away from you

Given a person's id and a target friendship level, return all videos watched by people at that exact level, sorted by frequency (ascending) and alphabetically for ties. This is essentially finding videos in your social network at a specific "distance" using graph traversal!

Input & Output

example_1.py โ€” Basic Social Network
$ Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
โ€บ Output: ["B","C"]
๐Ÿ’ก Note: Person 0's friends are [1,2]. Person 1 watched ["C"], person 2 watched ["B","C"]. Videos: B(1), C(2). Sorted by frequency: ["B","C"]
example_2.py โ€” Level 2 Friends
$ Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
โ€บ Output: ["D"]
๐Ÿ’ก Note: Person 0's level 2 friends are [3]. Person 3 watched ["D"]. Result: ["D"]
example_3.py โ€” Self (Level 0)
$ Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 0
โ€บ Output: ["A","B"]
๐Ÿ’ก Note: Level 0 means the person themselves. Person 0 watched ["A","B"]. Sorted alphabetically: ["A","B"]

Visualization

Tap to expand
Social Network Video DiscoveryYOULevel 0F1F2F3Level 1 (Friends)FF1FF2FF3FF4Level 2 (Friends of Friends)Video Collection ProcessLevel 1F1: ["Action", "Comedy"]F2: ["Drama"]F3: ["Comedy", "Horror"]Count FrequenciesAction: 1Comedy: 2Drama: 1, Horror: 1Sort ResultsBy frequency: 1, 1, 1, 2Then alphabetical:["Action","Drama","Horror","Comedy"]ComplexityTime: O(n+m+v log v)Space: O(n+v)Optimal Solution
Understanding the Visualization
1
Start at You
Begin BFS from your position (level 0)
2
Find Direct Friends
Explore all your direct friends (level 1)
3
Expand Outward
Continue to friends of friends (level 2, 3, ...)
4
Collect Videos
At target level, gather all videos and sort by frequency
Key Takeaway
๐ŸŽฏ Key Insight: BFS naturally explores the graph level by level, making it perfect for finding people at exactly k friendship connections away. This eliminates the need to calculate distances individually and provides optimal performance.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + m + v log v)

BFS takes O(n + m) where n is people and m is friendships, plus O(v log v) for sorting videos

n
2n
โšก Linearithmic
Space Complexity
O(n + v)

Queue and visited set take O(n), video frequency map takes O(v)

n
2n
โšก Linearithmic Space

Constraints

  • n == friends.length == watchedVideos.length
  • 2 โ‰ค n โ‰ค 100
  • 1 โ‰ค friends[i].length โ‰ค n - 1
  • 0 โ‰ค friends[i][j] โ‰ค n - 1
  • 0 โ‰ค id < n
  • 1 โ‰ค level โ‰ค n - 1
  • All values in friends[i] are unique
  • The number of videos watched by each person is at most 100
Asked in
Facebook 45 Amazon 32 Google 28 Microsoft 22
31.2K Views
Medium Frequency
~18 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen