Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your design should support the following methods:
postTweet(userId, tweetId): Compose a new tweet.
getNewsFeed(userId): Retrieve the 10 most recent tweet ids in the user’s news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.
follow(followerId, followeeId): Follower follows a followee.
unfollow(followerId, followeeId): Follower unfollows a followee.
Example:
解法1: Heap
用OOD设计的方法解这题比较清楚明白。在设计getNewsFeed
的时候,用一个priorityQueue来把每一个follow的user的tweet的head放入堆中,按照时间顺序出堆入堆。每出一个就把当前指向的tweet的下一个推入堆中。
|
|