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

Visualization

Tap to expand
Video Sharing Platform ArchitectureUploadNew VideoID ManagerMin-HeapVideo StoreHash MapAnalyticsEngagementWatchLike/DislikeRemoveTime Complexities:โ€ข Upload: O(log n)โ€ข Watch: O(1)โ€ข Like/Dislike: O(1)โ€ข Remove: O(log n)โ€ข Get Stats: O(1)
Understanding the Visualization
1
Smart ID Assignment
Use min-heap to always assign the smallest available ID
2
Fast Video Access
Hash map provides instant access to any video by ID
3
Engagement Tracking
Separate stats tracking for views, likes, and dislikes
4
Efficient Cleanup
Deleted video IDs return to the available pool
Key Takeaway
๐ŸŽฏ Key Insight: Combining min-heap for ID management with hash maps for data storage creates a scalable video platform that efficiently handles millions of videos while maintaining optimal performance for all operations.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(log n) for upload, O(1) for other operations

Heap operations for ID management, direct hash access for video operations

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for heap, hash maps, and video content storage

n
2n
โšก Linearithmic Space

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
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