Skip to main content
Back to problems
Leetcode
Medium
Arrays
Graphs
Hash Maps
Sorting
Get Watched Videos By Your Friends

Recommend videos watched by friends at a given friendship distance, ordered by frequency and then name.

Acceptance 0%
Problem Statement

Given a list of each person's watched videos and an undirected friendship graph, find the videos watched by people who are exactly kk friendship steps away from a given person.

Collect all videos watched by those friends, count how many times each video appears, and return the videos sorted by:

  1. increasing frequency,
  2. lexicographically increasing video name for ties.

This is a graph traversal and aggregation problem that combines distance-limited search with frequency-based sorting.

Input Format

  • An array watchedVideos, where watchedVideos[i] is the list of videos watched by person i.
  • An adjacency list friends, where friends[i] contains the direct friends of person i.
  • An integer id for the starting person.
  • An integer k for the friendship distance.

Output Format

  • Return a list of video titles watched by people whose shortest friendship distance from id is exactly k.
  • The result must be sorted by frequency ascending, then by title ascending.

Constraints

  • The friendship graph is undirected.
  • 0 <= id < n
  • k >= 0
  • Video titles are compared lexicographically using standard string ordering.
  • The exact original platform constraints are not assumed here; solve under typical interview-scale limits.
Examples
Sample cases returned by the problem API.

Example 1

Input

watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, k = 2

Output

["D"]

Explanation

At distance 2 from person 0 is person 3. Person 3 watched only video D, so the answer is ["D"].

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.