The Get Watched Videos by Your Friends interview question asks you to find the videos watched by your friends at a specific "level" of connection. Level 1 friends are your direct friends. Level 2 friends are the friends of your Level 1 friends (excluding yourself and Level 1 friends). You need to return a list of all videos watched by people at exactly the given level, sorted by their frequency of occurrence (ascending), and then alphabetically by video name.
Amazon uses this Graph interview pattern to test your ability to perform a constrained Breadth-First Search (BFS). It evaluates if you know how to stop a search at a specific depth and how to avoid cycles (not revisiting friends). It also tests your ability to aggregate and sort data using custom comparators.
This problem combines BFS, Hash Table Counting, and Sorting.
id. Use a queue and a visited set to perform a level-by-level BFS. Stop when you reach the target level.watchedVideos of these target friends and count their frequencies using a Hash Map.id = 0, level = 2
Friends graph: 0 -> 1, 0 -> 2, 1 -> 3, 2 -> 3.
Videos: [ ["A"], ["B"], ["C"], ["D", "B"] ]
[0]. Visited {0}.[1, 2]. Visited {0, 1, 2}.[3].{"D": 1, "B": 1}.["B", "D"].visited set, which causes infinite loops or incorrectly placing a Level 1 friend into the Level 3 bucket because of a cycle.When BFS requires stopping at a specific level, always use a for loop inside your while(queue) loop that runs exactly queue.size() times. This perfectly captures all nodes at the current depth before moving to the next.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Count Covered Buildings | Medium | Solve | |
| Minimum Index of a Valid Split | Medium | Solve | |
| Minimum Jumps to Reach Home | Medium | Solve | |
| Analyze User Website Visit Pattern | Medium | Solve | |
| Arithmetic Subarrays | Medium | Solve |