YouTube Subscribe Feature

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-09-04

Problem Statement

Design the subscription feature of YouTube, where a user can subscribe/unsubscribe to channels and receive updates about new content. The focus is on object-oriented design, modularity, and efficient data structures for managing subscriptions at scale.


✅ Functional Requirements


1.Subscribe / Unsubscribe
•Users can subscribe to a channel.
•Users can unsubscribe from a channel.
2.Subscription Management
•Maintain a list of subscribed channels per user.
•Maintain a list of subscribers per channel.
3.Notifications / Updates
•When a channel uploads a new video, notify its subscribers (push or pull model).
•Support querying recent uploads from subscribed channels.
4.Optional Features
•Track subscription count per channel.
•Support privacy options (public/private subscriptions).
•Enable recommendations based on subscriptions.


🔐 Constraints & Assumptions


•The system should support millions of users and channels.
•Subscription lists should support fast add/remove and lookup.
•Notifications can be batched or delivered asynchronously.
•Focus is on LLD and modular design, not the full YouTube platform.


🧱 Key Components & Classes


1.User Class
1 2class User { 3private: 4 int userId; 5 string name; 6 unordered_set<Channel*> subscriptions; // channels user is subscribed to 7public: 8 void subscribe(Channel* channel); 9 void unsubscribe(Channel* channel); 10 vector<Video*> getSubscribedVideos(); // optional: fetch recent uploads 11};
1.Channel Class
1 2class Channel { 3private: 4 int channelId; 5 string name; 6 unordered_set<User*> subscribers; 7 vector<Video*> videos; 8public: 9 void addSubscriber(User* user); 10 void removeSubscriber(User* user); 11 void uploadVideo(Video* video); 12 int getSubscriberCount(); 13};
1.Video Class (Optional)
1 2class Video { 3private: 4 int videoId; 5 string title; 6 DateTime uploadTime; 7public: 8 // Video metadata 9};
1.NotificationService (Optional)
•Sends updates to subscribers when a new video is uploaded.
1 2class NotificationService { 3public: 4 void notifySubscribers(Channel* channel, Video* video); 5};


🔄 Example Workflow


1.User subscribes to a channel:
•user.subscribe(channel) → adds channel to user’s subscription list → adds user to channel’s subscriber list.
2.Channel uploads a video:
•channel.uploadVideo(video) → NotificationService notifies all subscribers.
3.User unsubscribes:
•Removes channel from user subscriptions and user from channel subscribers.


🧪 Expected Deliverables


•Class diagram showing User, Channel, Video, and NotificationService.
•Implementation of subscribe/unsubscribe logic and subscriber management.
•Test cases:
•Subscribing/unsubscribing users
•Uploading videos and notifying subscribers
•Querying recent videos from subscriptions


💡 Discussion Points


•Trade-offs: unordered_set vs list for subscription storage.
•Performance: fast lookup for checking subscriptions and notifying subscribers.
•Scalability: support millions of users and channels efficiently.
•Optional extensions: batch notifications, recommendations, or subscription privacy settings.


Solution

Comments