Practice/Google/Design Twitter
Object Oriented DesignMust
Design and implement a simplified social media feed system that supports core functionality similar to platforms like Twitter. Your system should allow users to create posts, establish follow relationships with other users, and retrieve a personalized feed showing the most recent posts from themselves and the people they follow.
The system needs to efficiently handle the creation of posts, management of follower relationships, and generation of personalized feeds that display content in reverse chronological order (newest first).
post_message(user_id, message) method that creates a new post for a given userfollow(follower_id, followee_id) method that establishes a following relationshipunfollow(follower_id, followee_id) method that removes a following relationshipget_feed(user_id, count) method that returns the most recent posts visible to a userExample 1: Basic posting and feed retrieval
` SocialFeed system = new SocialFeed() system.post_message(1, "Hello, world!") system.post_message(1, "Having a great day!") system.get_feed(1, 10)
Output: ["Having a great day!", "Hello, world!"] Explanation: User 1's feed contains their own posts in reverse chronological order `
Example 2: Following another user
` SocialFeed system = new SocialFeed() system.post_message(1, "User 1 post") system.post_message(2, "User 2 post") system.follow(1, 2) system.get_feed(1, 10)
Output: ["User 2 post", "User 1 post"] Explanation: User 1 follows User 2, so their feed includes posts from both users `
Example 3: Unfollowing and feed updates
` SocialFeed system = new SocialFeed() system.follow(1, 2) system.post_message(2, "Before unfollow") system.unfollow(1, 2) system.post_message(2, "After unfollow") system.get_feed(1, 10)
Output: ["Before unfollow"] Explanation: Posts created before unfollowing remain, but new posts after unfollowing don't appear `
Example 4: Feed with multiple follows
` SocialFeed system = new SocialFeed() system.post_message(1, "Post A") system.post_message(2, "Post B") system.post_message(3, "Post C") system.follow(1, 2) system.follow(1, 3) system.get_feed(1, 10)
Output: ["Post C", "Post B", "Post A"] Explanation: Feed aggregates posts from user 1, 2, and 3 in chronological order `