This is one of my java interview questions...
I was asked which data structure would you use to implement Facebook post, like and comments?
I would love to hear the view of all the members....
This is one of my java interview questions...
I was asked which data structure would you use to implement Facebook post, like and comments?
I would love to hear the view of all the members....
This is really more a task for a database (notice how complex the below is, compared to this (because all of the below is taken care of with a proper database with proper indexing)), but...
Note I'm assuming only posts can be liked and comments are for a post.
Posts:
Posts can probably be a Map of post ID to post object.
If you want to see a list of posts a user made, you'll also need a Map of user ID to a Set of post IDs.
Likes:
You want to quickly get the number of likes per post and see whether a user liked a post, so a Map of post ID to a Set of user IDs of those who like it.
If you also want to quickly see which things a user likes, you'll need a another data structure - a Map of user ID to a Set of post IDs liked.
Comments:
Comments can probably be a Map of comment ID to comment object. The connected post ID will be stored in the comment object.
You want to quickly get the comments per post, so a Map of post ID to a Set of comment IDs.
If you also want to quickly see a list of comments made by a user, you'll need a another data structure - a Map of user ID to a Set of comment IDs.