Design Video Sharing Platform - Problem

Design a comprehensive video sharing platform similar to YouTube or TikTok that supports core functionalities like uploading, deleting, watching, and rating videos.

Each video is represented as a string of digits, where the i-th digit represents the content at minute i. For example, video "12345" has content '1' at minute 0, '2' at minute 1, and so on.

Key Features:

  • Smart ID Management: Videos get the smallest available ID starting from 0. Deleted video IDs can be reused.
  • Video Streaming: Users can watch specific time ranges of videos
  • Engagement Tracking: Track views, likes, and dislikes for each video
  • Content Management: Upload and remove videos dynamically

Implement the VideoSharingPlatform class with methods for upload, remove, watch, like, dislike, and analytics retrieval. The platform should efficiently handle video ID recycling and maintain accurate engagement metrics.

Input & Output

example_1.py — Basic Operations
$ Input: ["VideoSharingPlatform", "upload", "upload", "remove", "upload", "watch", "like", "dislike", "getLikesAndDislikes", "getViews"] [[], ["123"], ["456"], [0], ["789"], [1, 0, 1], [1], [1], [1], [1]]
Output: [null, 0, 1, null, 0, "45", null, null, [1, 1], 1]
💡 Note: Upload "123" gets ID 0, upload "456" gets ID 1, remove video 0, upload "789" reuses ID 0, watch video 1 from minute 0-1 returns "45", like and dislike video 1, then get stats
example_2.py — Edge Cases
$ Input: ["VideoSharingPlatform", "upload", "watch", "watch", "watch"] [[], ["12345"], [0, 0, 2], [0, 3, 10], [0, 10, 15]]
Output: [null, 0, "123", "45", ""]
💡 Note: Upload video "12345", watch minutes 0-2 returns "123", watch minutes 3-10 returns "45" (clamped to video length), watch beyond video length returns empty string
example_3.py — Invalid Operations
$ Input: ["VideoSharingPlatform", "watch", "like", "getLikesAndDislikes", "getViews"] [[], [999, 0, 1], [999], [999], [999]]
Output: [null, "-1", null, [-1], -1]
💡 Note: Operations on non-existent video ID 999 return appropriate error values: "-1" for watch, [-1] for stats, -1 for views

Constraints

  • 1 ≤ video.length ≤ 105
  • The sum of video.length over all calls to upload does not exceed 105
  • video consists of digits
  • 0 ≤ startMinute < endMinute < 105
  • 0 ≤ videoId < 105
  • At most 104 calls will be made to all functions combined
  • All video content must consist only of digit characters

Visualization

Tap to expand
Video Sharing Platform Design INPUT Operations: 1. upload("123") 2. upload("456") 3. remove(0) 4. upload("789") 5. watch(1, 0, 1) 6. like(1) 7. dislike(1) 8. getLikesAndDislikes(1) 9. getViews(1) Hash Map Structure: ID Video Data Stats ALGORITHM STEPS 1 ID Management Use min-heap for smallest available ID reuse 2 Video Storage HashMap: ID --> content, views, likes, dislikes 3 Watch Operation Substring extraction Increment view count 4 Engagement Tracking Track likes/dislikes per video ID State After Operations: ID: 0 "789" (reused) ID: 1 "456" v:1 L:1 D:1 ID: 2 next FINAL RESULT Operation Results: upload("123") --> 0 upload("456") --> 1 remove(0) --> null (deleted) upload("789") --> 0 (reused!) watch(1,0,1) --> "45" like(1) --> null dislike(1) --> null getLikesAndDislikes(1) --> [1,1] getViews(1) --> 1 Output Array: [null, 0, 1, null, 0, "45", null, null, [1,1], 1] OK - All operations verified Key Insight: Use a HashMap for O(1) video access and a Min-Heap (priority queue) to efficiently manage reusable IDs. When a video is deleted, push its ID to the heap. When uploading, pop from the heap if available, otherwise use the next sequential ID. This ensures smallest ID reuse. TutorialsPoint - Design Video Sharing Platform | Hash Map + Min-Heap Approach
Asked in
YouTube 85 Netflix 72 TikTok 68 Amazon Prime 45 Meta 38
67.5K Views
High Frequency
~35 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen