Design Video Sharing Platform - Solution & Explanation
Problem Statement
You have a video sharing platform where users can upload and delete videos. Each video is a string of digits, where the ith digit of the string represents the content of the video at minute i. For example, the first digit represents the content at minute 0 in the video, the second digit represents the content at minute 1 in the video, and so on. Viewers of videos can also like and dislike videos. Internally, the platform keeps track of the number of views, likes, and dislikes on each video.
When a video is uploaded, it is associated with the smallest available integer videoId starting from 0. Once a video is deleted, the videoId associated with that video can be reused for another video.
Implement the VideoSharingPlatform class:
VideoSharingPlatform()Initializes the object.int upload(String video)The user uploads avideo. Return thevideoIdassociated with the video.void remove(int videoId)If there is a video associated withvideoId, remove the video.String watch(int videoId, int startMinute, int endMinute)If there is a video associated withvideoId, increase the number of views on the video by1and return the substring of the video string starting atstartMinuteand ending atmin(endMinute, video.length - 1)(inclusive). Otherwise, return"-1".void like(int videoId)Increases the number of likes on the video associated withvideoIdby1if there is a video associated withvideoId.void dislike(int videoId)Increases the number of dislikes on the video associated withvideoIdby1if there is a video associated withvideoId.int[] getLikesAndDislikes(int videoId)Return a 0-indexed integer arrayvaluesof length2wherevalues[0]is the number of likes andvalues[1]is the number of dislikes on the video associated withvideoId. If there is no video associated withvideoId, return[-1].int getViews(int videoId)Return the number of views on the video associated withvideoId, if there is no video associated withvideoId, return-1.
Example 1:
Input
["VideoSharingPlatform", "upload", "upload", "remove", "remove", "upload", "watch", "watch", "like", "dislike", "dislike", "getLikesAndDislikes", "getViews"]
[[], ["123"], ["456"], [4], [0], ["789"], [1, 0, 5], [1, 0, 1], [1], [1], [1], [1], [1]]
Output
[null, 0, 1, null, null, 0, "456", "45", null, null, null, [1, 2], 2]
Explanation
VideoSharingPlatform videoSharingPlatform = new VideoSharingPlatform();
videoSharingPlatform.upload("123"); // The smallest available videoId is 0, so return 0.
videoSharingPlatform.upload("456"); // The smallest available videoId is 1, so return 1.
videoSharingPlatform.remove(4); // There is no video associated with videoId 4, so do nothing.
videoSharingPlatform.remove(0); // Remove the video associated with videoId 0.
videoSharingPlatform.upload("789"); // Since the video associated with videoId 0 was deleted,
// 0 is the smallest available videoId, so return 0.
videoSharingPlatform.watch(1, 0, 5); // The video associated with videoId 1 is "456".
// The video from minute 0 to min(5, 3 - 1) = 2 is "456", so return "456".
videoSharingPlatform.watch(1, 0, 1); // The video associated with videoId 1 is "456".
// The video from minute 0 to min(1, 3 - 1) = 1 is "45", so return "45".
videoSharingPlatform.like(1); // Increase the number of likes on the video associated with videoId 1.
videoSharingPlatform.dislike(1); // Increase the number of dislikes on the video associated with videoId 1.
videoSharingPlatform.dislike(1); // Increase the number of dislikes on the video associated with videoId 1.
videoSharingPlatform.getLikesAndDislikes(1); // There is 1 like and 2 dislikes on the video associated with videoId 1, so return [1, 2].
videoSharingPlatform.getViews(1); // The video associated with videoId 1 has 2 views, so return 2.
Example 2:
Input ["VideoSharingPlatform", "remove", "watch", "like", "dislike", "getLikesAndDislikes", "getViews"] [[], [0], [0, 0, 1], [0], [0], [0], [0]] Output [null, null, "-1", null, null, [-1], -1] Explanation VideoSharingPlatform videoSharingPlatform = new VideoSharingPlatform(); videoSharingPlatform.remove(0); // There is no video associated with videoId 0, so do nothing. videoSharingPlatform.watch(0, 0, 1); // There is no video associated with videoId 0, so return "-1". videoSharingPlatform.like(0); // There is no video associated with videoId 0, so do nothing. videoSharingPlatform.dislike(0); // There is no video associated with videoId 0, so do nothing. videoSharingPlatform.getLikesAndDislikes(0); // There is no video associated with videoId 0, so return [-1]. videoSharingPlatform.getViews(0); // There is no video associated with videoId 0, so return -1.
Constraints:
1 <= video.length <= 105- The sum of
video.lengthover all calls touploaddoes not exceed105 videoconsists of digits.0 <= videoId <= 1050 <= startMinute < endMinute < 105startMinute < video.length- The sum of
endMinute - startMinuteover all calls towatchdoes not exceed105. - At most
105calls in total will be made to all functions.
Approach Overview
Problem Overview: You need to design a simplified video sharing platform that supports uploading videos, deleting them, watching a substring of the video, and tracking likes, dislikes, and views. The tricky part is reusing the smallest available video ID after deletions while keeping all operations efficient.
Approach 1: Sequential ID Scan with Hash Table (O(n) allocation, O(1) average ops)
A straightforward design stores videos in a hash map where the key is the video ID and the value stores the video string plus metadata (views, likes, dislikes). When uploading, scan from 0 upward to find the first unused ID. Operations like watch, like, and dislike become constant-time hash lookups. The downside is the upload step: scanning for the smallest free ID can degrade to O(n) time when many IDs are occupied. This works for small datasets but does not scale well when uploads and deletions are frequent.
Approach 2: Hash Table + Ordered Set / Min-Heap for Reusable IDs (O(log n) per update)
A better design keeps two structures: a hash table mapping video IDs to video objects, and an ordered set (or min-heap) storing IDs freed after deletions. When uploading, check the ordered set first. If it contains IDs, remove the smallest one and reuse it; otherwise assign the next sequential ID. Deleting a video removes it from the hash map and inserts its ID into the ordered set for reuse.
Each stored video keeps the raw video string and counters for views, likes, and dislikes. The watch operation extracts a substring from startMinute to endMinute and increments the view counter. Like/dislike operations update counters directly. Hash lookups give O(1) average time for video access, while ID reuse costs O(log n) due to ordered set operations. This design matches the constraints of most system design style problems.
Some implementations replace the ordered set with a reusable ID stack if the problem does not require the smallest ID specifically. However, this problem requires the smallest available ID, making a min-heap or ordered set the correct choice.
Recommended for interviews: The hash table + ordered set approach. The brute sequential scan shows you understand the ID reuse requirement, but interviewers expect you to optimize allocation using a min-heap or ordered set. It keeps uploads and deletions efficient while maintaining constant-time video operations, which demonstrates strong design thinking.
Solutions for this problem are being prepared.
Try solving it yourselfDetailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sequential ID Scan + Hash Map | Upload O(n), other ops O(1) | O(n) | Simple implementation when uploads are small and ID reuse frequency is low |
| Hash Map + Min-Heap / Ordered Set | Upload/Delete O(log n), access O(1) | O(n) | General optimal solution when IDs must be reused in ascending order |
| Hash Map + Stack for Recycled IDs | O(1) | O(n) | When smallest-ID ordering is not required and constant-time reuse is preferred |
Video Solution
2254. Design Video Sharing Platform (Leetcode Hard) • Programming Live with Larry • 482 views views
Watch 1 more video solutions →Frequently Asked Questions
Is Design Video Sharing Platform easy or hard?
Design Video Sharing Platform Python/Java solution
How to solve Design Video Sharing Platform in O(log n)?
What is the best approach for Design Video Sharing Platform?
Is Design Video Sharing Platform asked at Google/Amazon/Meta?
What data structure is used in Design Video Sharing Platform?
What is the time complexity of Design Video Sharing Platform?
Ready to solve this problem?
Practice Design Video Sharing Platform with our built-in code editor and test cases.
Practice on FleetCode