65 / 75
Use HashMap for follows/tweets, merge k sorted lists with heap for news feed.
Example:
Input:
postTweet(1,5), follow(1,2), postTweet(2,6), getNewsFeed(1)Output:
[6,5] (most recent first)Common Mistakes:
- Not using timestamp for ordering tweets
- Inefficient news feed (O(n) scan when heap can optimize)
- Not including user's own tweets in news feed
- Allowing user to follow/unfollow themselves
Notes:
postTweet/follow/unfollow: O(1). getNewsFeed: O(n log n) where n=total tweets. Can optimize with better data structures.
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Use HashMap for follows/tweets, merge k sorted lists with heap for news feed.
Example:
Input:
postTweet(1,5), follow(1,2), postTweet(2,6), getNewsFeed(1)Output:
[6,5] (most recent first)Common Mistakes:
- Not using timestamp for ordering tweets
- Inefficient news feed (O(n) scan when heap can optimize)
- Not including user's own tweets in news feed
- Allowing user to follow/unfollow themselves
Notes:
postTweet/follow/unfollow: O(1). getNewsFeed: O(n log n) where n=total tweets. Can optimize with better data structures.
65/75
Design Twitter