We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Most Popular Video Creator

Number: 2543

Difficulty: Medium

Paid? No

Companies: TikTok


Problem Description

Given three arrays — creators, ids, and views — each video on a platform is represented by these arrays where creators[i] created a video with id ids[i] that got views[i] views. The popularity of a creator is defined as the sum of views on all of their videos. The task is to find the creator(s) with the highest popularity and, for each such creator, determine the id of their most popular video. In case of ties in the video view count, the lexicographically smallest id should be used.


Key Insights

  • Use a hash map to accumulate the total number of views (popularity) for each creator.
  • Use another hash map to track the best video (maximum views and lexicographically smallest id on tie) for each creator.
  • First pass: iterate through the videos once to update total views and best video information per creator.
  • Second pass: determine the maximum popularity among all creators and collect the results.

Space and Time Complexity

Time Complexity: O(n), where n is the number of videos, as we process each video once. Space Complexity: O(k), where k is the number of unique creators.


Solution

We solve the problem by iterating over the input arrays exactly once. As we iterate, we update two data structures:

  1. A dictionary (or hash map) that maps each creator to the total views from all their videos.
  2. A dictionary (or hash map) that maps each creator to a pair consisting of the highest view count seen and the corresponding video id. When a video’s view count ties with the current best, we update the stored id only if the new id is lexicographically smaller. Finally, we extract the maximum popularity value, and then for each creator with that popularity, add the creator and the id of their best video to the result.

Code Solutions

Below are implementations in Python, JavaScript, C++, and Java with line-by-line comments.

# Function to find the most popular creator(s) and their best video id.
def mostPopularCreator(creators, ids, views):
    # Map to store total views (popularity) for each creator.
    popularity = {}
    # Map to store the best video info for each creator as (max_views, best_id).
    best_video = {}

    # Process each video in one pass.
    for i in range(len(creators)):
        creator = creators[i]
        video_id = ids[i]
        view = views[i]

        # Update the total popularity for this creator.
        popularity[creator] = popularity.get(creator, 0) + view

        # Update the best video for this creator.
        if creator not in best_video:
            best_video[creator] = (view, video_id)
        else:
            current_max, current_id = best_video[creator]
            # If current video has more views, update.
            if view > current_max:
                best_video[creator] = (view, video_id)
            # For tied views, update with lexicographically smaller id.
            elif view == current_max and video_id < current_id:
                best_video[creator] = (view, video_id)

    # Find the maximum popularity among all creators.
    max_popularity = max(popularity.values())

    # Extract the result for creator(s) matching the maximum popularity.
    result = []
    for creator in popularity:
        if popularity[creator] == max_popularity:
            result.append([creator, best_video[creator][1]])
    return result

# Example usage:
creators = ["alice", "bob", "alice", "chris"]
ids = ["one", "two", "three", "four"]
views = [5, 10, 5, 4]
print(mostPopularCreator(creators, ids, views))  # Expected output: [["alice", "one"], ["bob", "two"]]
← Back to All Questions