Design a nested comment service with ordered replies and reply counts
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a nested comment service that manages comments on a post. Comments can be top-level or replies to existing comments, forming a tree structure that can be nested to any depth.
The service must efficiently support adding comments, replying to comments, retrieving the entire comment tree in a structured nested format, and counting the total number of replies (all descendants) for any given comment.
Each comment has a unique integer `commentId` and a `text` string. The comment tree should preserve insertion order for both top-level comments and replies at every level.
Implement a class `NestedCommentService` with the following methods:
- `addComment(commentId: int, text: str)`: Add a top-level comment to the post with the given ID and text.
- `addReply(commentId: int, parentId: int, text: str)`: Add a reply with the given ID and text as a child of the comment with `parentId`.
- `getCommentTree() -> List`: Return the full comment tree as a nested list. Each comment is `[id, text, [children...]]`. Top-level comments and children are in insertion order.
- `getReplyCount(commentId: int) -> int`: Return the total number of replies (all descendants at every level) for the given comment.
- `getComment(commentId: int) -> str`: Return the text of the comment with the given ID, or an empty string if not found.
For example:
```text
addComment(1, "Great post")
addComment(2, "Thanks for sharing")
addReply(3, 1, "Agreed")
addReply(4, 3, "Same here")
addReply(5, 1, "I have a question")
getCommentTree() -> [
[1, "Great post", [
[3, "Agreed", [
[4, "Same here", []]
]],
[5, "I have a question", []]
]],
[2, "Thanks for sharing", []]
]
getReplyCount(1) -> 3
getReplyCount(3) -> 1
getReplyCount(2) -> 0
getComment(4) -> "Same here"
getComment(42) -> ""
```
Comment 1 has two direct replies (3 and 5) and one reply to a reply (4), so its count is 3. Comment 3 comes before comment 5 under comment 1 because it was added first.
```hint Reaching the parent
`addReply` only receives the parent's ID. Work out how to reach that parent without searching the tree, and how each comment should hold its replies so their order survives.
```
```hint Counting replies
The count includes replies to replies. Think about how often counts are read compared with how often replies are added, and where that pushes the cost.
```
### Constraints & Assumptions
- `1 <= commentId <= 10^4`, and all `commentId` values are unique across all calls.
- `1 <= len(text) <= 200`
- `parentId` in `addReply` always refers to an existing comment.
- `getComment` may be called with an ID that does not exist.
- At most `1000` operations in total, so a single thread can be close to 1000 levels deep.
### Clarifying Questions to Ask
- What should `getReplyCount` return for an ID that does not exist: 0, or an error?
- Are reply counts read far more often than replies are written? That decides whether to compute them on demand or keep them updated.
- How deep can a thread get in practice? That decides whether recursive traversal is safe.
- Can comments be edited or deleted later, and if one is deleted, what happens to its replies?
### What a Strong Answer Covers
- A storage layout that finds any comment by ID in constant time and keeps every comment's replies in insertion order.
- Building the nested `[id, text, [children...]]` output with the right order at every level.
- Counting all descendants, with the cost of computing on read compared with maintaining counts on write.
- Behavior for very deep threads and for unknown IDs.
- Time and space complexity of each method.
### Follow-up Questions
- How would you add deletion, and what happens to the replies and to the counts stored on every ancestor?
- For a post with 100,000 comments, how would you return only the first page of top-level comments with a few replies each?
- How would you store this in a database and load a post's whole tree in one query?
- If many users reply to the same thread at once, how do you keep the reply counts correct?
Overview: Design a service that stores comments and replies to any depth, returns the whole thread as a nested list in insertion order, and counts every reply under a given comment. It tests tree modeling, ordered child storage, subtree traversal, and the cost of computing counts on read compared with keeping them updated on write.