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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code