Describe how you would implement a forum such as Reddit? Be sure to describe all relevant models and their relationships to each other.
Where: I was asked this problem for an interview for BuildZoom.
First of all, I would like to identify the goal and requirements. I want to create a system that will hold many posts that anybody can access if they go to the system's URL. A user can create many posts while other users can view and can optionally choose to either upvote/downvote. The posts with the most upvotes will appear on the "front page". Posts should be able to handle comments from other users and comments can have replies.
User (id, email, username, name, etc.)
Comment (id, text, post_id, user_id)
Post (id, user_id, url)
Picture (id, post_id, url)
Vote (id, post_id, comment_id, count)
Then I would try to explain the relationships between the classes/models:
User has many posts.
Post has many comments. Post belongs to user.
Post has many pictures.
Comment has many comments (replies). self relationship
Comment belongs to Post. Comment belongs to User through Post.
Post has one vote.
Comment has one vote.
Vote belongs to either Post or Comment.
Webpage will have a feed of posts created by different users. As a user you have the ability to create a post, interact with other posts, have notifications about replies to your comments, access a history page of your comments / posts.
To store everything I would set up a relational database in the backend. Traffic can be load balanced on different servers based on location. As we scale, we can add more servers to more locations and this can be achieved quite easily nowadays due to cloud computing services such as AWS.
Followup question: How would you improve page load performance? For example, a post might have many hundreds of comments or even a single comment might have hundreds of replies or we could have a chain of replies on a single comment..
My answer: I would implement some form of lazy-loading in which i only load upon scroll or by clicking "See more comments" etc. There will be a cap on the number of replies shown for each comment for example, 3 comments, before I force a user to click the "See more comments" text.
Other topics you could expand on depending on the interviewer:
- other features your service could offer (i.e sorting by Hot, New etc.)
- more in-depth into databases (i didn't know what else to say beyond relational which I don't think Reddit actually uses)
- platforms service will be offered on (web browser, mobile, etc.)