Practice/Coupang/Leetcode 355. Design Twitter
Leetcode 355. Design Twitter
CodingMust
Problem
Design and implement a social media feed system that supports the following operations:
- post(userId, tweetId): Create a new post with the given tweet ID by the specified user
- getFeed(userId): Retrieve the 10 most recent tweet IDs visible in the user's news feed. The feed includes the user's own tweets and tweets from users they follow, sorted by recency (most recent first)
- follow(followerId, followeeId): The follower user begins following the followee user
- unfollow(followerId, followeeId): The follower user stops following the followee user
Your implementation should efficiently handle the merging of multiple tweet streams to generate a user's personalized feed.
Requirements
- Each user automatically sees their own posts in their feed (users implicitly follow themselves)
- The getFeed operation must return at most 10 tweet IDs
- Tweets should be ordered by the time they were posted (most recent first)
- A user cannot follow themselves explicitly (this operation should be ignored)
- Unfollowing a user who isn't currently followed should not cause an error
- Tweet IDs are unique and serve as timestamps (higher ID means more recent)
Constraints
- 1 ≤ userId, followerId, followeeId ≤ 500
- 0 ≤ tweetId ≤ 10^4
- At most 3 × 10^4 calls will be made to post, getFeed, follow, and unfollow
- All tweet IDs are unique and given in strictly increasing order
Examples
Example 1:
`
Operations: ["SocialFeed", "post", "getFeed", "follow", "post", "getFeed"]
Arguments: [[], [1, 5], [1], [1, 2], [2, 6], [1]]
Output: [null, null, [5], null, null, [6, 5]]
Explanation:
- Initialize the system
- User 1 posts tweet 5
- User 1's feed shows [5] (their own post)
- User 1 follows user 2
- User 2 posts tweet 6
- User 1's feed shows [6, 5] (tweet 6 from followee is more recent)
`
Example 2:
`
Operations: ["SocialFeed", "post", "post", "post", "getFeed", "follow", "getFeed", "unfollow", "getFeed"]
Arguments: [[], [1, 1], [2, 2], [2, 3], [1], [1, 2], [1], [1, 2], [1]]
Output: [null, null, null, null, [1], null, [3, 2, 1], null, [1]]
Explanation:
- User 1 posts tweet 1
- User 2 posts tweets 2 and 3
- User 1's feed shows [1] (only their own post)
- User 1 follows user 2
- User 1's feed shows [3, 2, 1] (includes user 2's posts)
- User 1 unfollows user 2
- User 1's feed shows [1] (back to only their own post)
`