Design Nested Comments
💬 What is a nested comment section? It is the discussion under a post on a site like Reddit or Hacker News. People comment on the post, and anyone can reply to any comment, so the discussion grows into a tree. Readers sort it by votes or by time, and a big thread loads a little at a time, with "load more replies" links where the rest is hidden.
Interviewers ask this in a few forms: "design Reddit's comment section", "design threaded comments", or the same prompt with a focus on loading huge threads, on sorting, or on search. They are one design. The tree and the votes are the base; sorting is a choice we make at every level of the tree; eager and lazy loading are two ways to read that tree, and we support both; search is an index on the side. Each one gets its own section below.
Requirements
"Design the comment section for a post, like Reddit's. People can reply to any comment, and replies can be nested as deep as they like."
The prompt is short, so most of the design comes from the questions. These are the ones that change the code.
Clarifying Questions
You: How deep can replies go?
Interviewer: Any depth. But a reader never sees all of it at once.
You: How do we load a thread with thousands of comments?
Interviewer: The page asks for the first few levels and the first few replies at each level. Where something is cut off, it shows "load more", and clicking it fetches the next replies of that comment, a page at a time.
You: How can readers sort?
Interviewer: Top (by votes), new and old. The sort applies at every level: the top-level comments are sorted, and so are the replies under each one.
You: How do votes work?
Interviewer: Up or down, one vote per user per comment, and they can change it or take it back. Anyone can vote, the author included.
You: What happens when someone deletes a comment that has replies?
Interviewer: The replies must stay. The comment shows as [deleted], and nobody can reply to it or vote on it any more. Only the author can delete.
You: What should search do?
Interviewer: Find the comments under this post that contain a word, ignoring case. Deleted comments should not match.
You: Do we need timestamps for "new" and "old"?
Interviewer: No. Comments are numbered in the order they are written, so the id tells you which is newer.
The loading answer is the one that shapes the design. "Some levels, some replies per level, and load more where it is cut off" is neither fully eager nor fully lazy. We will build both reads and see why the mix is what real sites do.
💡 Interview tip: Tree problems hide a reading question. Before you design the classes, ask how the tree is read: all at once, one level at a time, or a window of it. The answer decides your methods, and it is usually the thing your interviewer wants to talk about.
Final Requirements
- A user can comment on the post or reply to any comment that is not deleted. Comments get ids
1, 2, 3, ...in order. - A user can vote a comment up or down, change the vote, or take it back; one vote per user per comment. A comment's score is upvotes minus downvotes.
- The author can delete their comment. It stays in the tree as
[deleted]with its replies; it can no longer be replied to or voted on. - Replies can be sorted by top (score, then oldest), new (newest first) or old (oldest first), at every level.
- Lazy loading: get one page of the direct replies to a comment (or to the post).
- Eager loading: get the subtree under a comment (or the post) down to a maximum depth, with at most N replies per comment, and a "more" marker wherever replies were left out.
- Search: the live comments that contain a word, ignoring case.
Out of scope
- Editing comments, moderation, user profiles and karma.
- Ranking with time decay ("hot", "best"); we come back to it in Extensibility.
- Persistence and many threads at once (also in Extensibility).