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.
Approach:
I began with a standard graph boilerplate. Then, I began to manipulate the basic graph operations to fit program in question.
Building the connections was simple, but the most creative operation getNewsFeed.
With getNewsFeed, I took the approach of only adding tweets the users made on their profiles. Within my graph, the first node (key) contains all the information about the user. The remaining link in (value) in the graph were merely identifiers on where to look for the key nodes. So to find the news feed, I would iterate through the list of the persons friends, hash back to the first key, and grab the tweets. Each tweet as an associated time stamp that I later use to sort by time.