Problem Description
Design a video sharing platform where users can upload, delete, watch, like, and dislike videos. Each video is represented as a string of digits, where each digit corresponds to the content at that minute. The platform assigns the smallest available videoId (starting from 0) upon upload, and when videos are removed, their videoIds become available for reuse. The system also tracks the number of views, likes, and dislikes per video.
Key Insights
- Use a min-heap (or priority queue) to efficiently track and assign the smallest available videoId when uploading a video.
- Store video information (video string, views, likes, dislikes) in a hash map (or dictionary) keyed by the videoId.
- Update the view count on each watch call and ensure proper substring extraction based on provided indices.
- Ignore operations (like, dislike, watch, removal) for invalid or non-existing videoIds by returning error indicators as specified.
Space and Time Complexity
Time Complexity:
- upload: O(log n) due to the heap operation for available videoIds (or O(1) if heap is empty).
- remove: O(log n) due to adding an id back into the heap.
- watch, like, dislike, getLikesAndDislikes, getViews: O(1) (plus O(k) for substring extraction where k is the substring length, but k summed over calls is limited). Space Complexity:
- O(V + L) where V is the number of active videos (storing video strings and counts) and L is the space needed for the min-heap of available videoIds.
Solution
We maintain a counter to assign new videoIds and use a min-heap to store freed videoIds when videos are removed. The active videos are stored in a hash map where each entry holds the video content string along with its view count, like count, and dislike count. For each operation:
- upload: Check the min-heap; if any id is available, reuse it. Otherwise, assign the next new id.
- remove: Delete the video from the hash map and add its id to the min-heap.
- watch: Validate the videoId; if exists, increment its view count, extract the required substring, and return it.
- like/dislike: Increment the corresponding counter for the video if it exists.
- getLikesAndDislikes/getViews: Return the stored counters if the video exists; otherwise, return an error indicator.
This design ensures constant time access for most operations while keeping track of reusable ids efficiently.