#2254
Hard Algorithms Design video sharing platform
Hash Table Design Heap (Priority Queue)
64.1% acceptance
Mar 31, 2026
88
15
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 a video. Return the videoId associated with the video.
void remove(int videoId) If there is a video associated with videoId, remove the video.
String watch(int videoId, int startMinute, int endMinute) If there is a video associated with videoId, increase the number of views on the video by 1 and return the substring of the video string starting at startMinute and ending at min(endMinute, video.length - 1) (inclusive). Otherwise, return "-1".
void like(int videoId) Increases the number of likes on the video associated with videoId by 1 if there is a video associated with videoId.
void dislike(int videoId) Increases the number of dislikes on the video associated with videoId by 1 if there is a video associated with videoId.
int[] getLikesAndDislikes(int videoId) Return a 0-indexed integer array values of length 2 where values[0] is the number of likes and values[1] is the number of dislikes on the video associated with videoId. If there is no video associated with videoId, return [-1].
int getViews(int videoId) Return the number of views on the video associated with videoId, if there is no video associated with videoId, return -1.
Solution
Rust
Time O(2^n)
Space O(n)
use std::collections::{BTreeSet, HashMap};
struct VideoData {
content: String,
views: i32,
likes: i32,
dislikes: i32,
}
struct VideoSharingPlatform {
available: BTreeSet<i32>,
next_id: i32,
videos: HashMap<i32, VideoData>,
}
impl VideoSharingPlatform {
fn new() -> Self {
VideoSharingPlatform {
available: BTreeSet::new(),
next_id: 0,
videos: HashMap::new(),
}
}
fn upload(&mut self, video: String) -> i32 {
let id = if let Some(&min_id) = self.available.iter().next() {
self.available.remove(&min_id);
min_id
} else {
let id = self.next_id;
self.next_id += 1;
id
};
self.videos.insert(id, VideoData {
content: video,
views: 0,
likes: 0,
dislikes: 0,
});
id
}
fn remove(&mut self, video_id: i32) {
if self.videos.remove(&video_id).is_some() {
self.available.insert(video_id);
}
}
fn watch(&mut self, video_id: i32, start_minute: i32, end_minute: i32) -> String {
if let Some(data) = self.videos.get_mut(&video_id) {
data.views += 1;
let end = std::cmp::min(end_minute as usize, data.content.len() - 1);
data.content[start_minute as usize..=end].to_string()
} else {
"-1".to_string()
}
}
fn like(&mut self, video_id: i32) {
if let Some(data) = self.videos.get_mut(&video_id) {
data.likes += 1;
}
}
fn dislike(&mut self, video_id: i32) {
if let Some(data) = self.videos.get_mut(&video_id) {
data.dislikes += 1;
}
}
fn get_likes_and_dislikes(&self, video_id: i32) -> Vec<i32> {
if let Some(data) = self.videos.get(&video_id) {
vec![data.likes, data.dislikes]
} else {
vec![-1]
}
}
fn get_views(&self, video_id: i32) -> i32 {
if let Some(data) = self.videos.get(&video_id) {
data.views
} else {
-1
}
}
}
// Your VideoSharingPlatform object will be instantiated and called as such:
// let obj = VideoSharingPlatform::new();
// let ret_1: i32 = obj.upload(video);
// obj.remove(videoId);
// let ret_3: String = obj.watch(videoId, startMinute, endMinute);
// obj.like(videoId);
// obj.dislike(videoId);
// let ret_6: Vec<i32> = obj.get_likes_and_dislikes(videoId);
// let ret_7: i32 = obj.get_views(videoId);