-2

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....

user1631651
  • 231
  • 3
  • 7
  • 9
  • click [here](http://stackoverflow.com/questions/7487476/facebook-like-data-structure) – HemChe Apr 18 '13 at 08:33
  • This article mainly deals with how data is getting stored in database... i am more concerned with how programmatically u can represent data so that it can be more optimized.... – user1631651 Apr 18 '13 at 09:43

1 Answers1

0

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.

Community
  • 1
  • 1
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138