Design a comment threading model with soft delete and reply chains
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a comment threading system that supports nested comments with parent-child relationships. Each comment has a unique integer `commentId`, an `author` string, and a `content` string.
The system should allow adding top-level comments, replying to existing comments to create nested threads, and deleting comments. When a comment is deleted, it is replaced with a placeholder (author becomes empty, content becomes `'[deleted]'`), but all of its replies are preserved in the tree.
The system should also support depth-first traversal of the entire comment tree and retrieving the reply chain (the path from the root comment down to any given comment).
Implement a class `CommentThread` with the following methods:
- `addComment(commentId: int, author: str, content: str)`: Add a new top-level comment with the given ID, author, and content.
- `addReply(commentId: int, parentId: int, author: str, content: str)`: Add a reply comment under the specified parent comment.
- `deleteComment(commentId: int) -> bool`: Mark a comment as deleted. Its content becomes `'[deleted]'` and its author becomes empty. Returns `True` if the comment was found and not already deleted, `False` otherwise. Replies are preserved.
- `getComment(commentId: int) -> List`: Return `[id, author, content]` for the given comment. Returns `[-1, '', '']` if the comment does not exist.
- `getReplyChain(commentId: int) -> List[List]`: Return the path from the root comment down to the specified comment as a list of `[id, author, content]`. Returns an empty list if the comment does not exist.
- `getAllComments() -> List[List]`: Return all comments in depth-first (pre-order) traversal order, each comment before its replies. Each entry is `[id, author, content, depth]`, where depth is the nesting level (0 for top-level). Top-level comments appear in the order they were added, and so do the replies under each comment.
For example:
```text
addComment(1, "alice", "First post")
addReply(2, 1, "bob", "I disagree")
addReply(3, 2, "carol", "Why?")
addComment(4, "dave", "Second post")
addReply(5, 1, "erin", "Nice post")
deleteComment(2) -> True
deleteComment(2) -> False
deleteComment(99) -> False
addReply(6, 2, "frank", "Replying anyway")
getComment(2) -> [2, "", "[deleted]"]
getComment(99) -> [-1, "", ""]
getReplyChain(6) -> [[1, "alice", "First post"], [2, "", "[deleted]"], [6, "frank", "Replying anyway"]]
getReplyChain(99) -> []
getAllComments() -> [[1, "alice", "First post", 0],
[2, "", "[deleted]", 1],
[3, "carol", "Why?", 2],
[6, "frank", "Replying anyway", 2],
[5, "erin", "Nice post", 1],
[4, "dave", "Second post", 0]]
```
Comment 2 keeps its place after it is deleted, so comment 3 still appears under it and comment 6 can still be attached to it. Comment 5 comes after comment 2's whole subtree because it was added to comment 1 later.
```hint Index by id
Every method except the full listing takes a single ID. Pick a structure that finds any comment in constant time, then decide which links each comment needs so you can move both up toward the root and down toward its replies.
```
```hint Deep threads
Within the operation limit, one thread can be a single chain of replies thousands of levels deep. Check that both the reply chain and the full traversal still work on that shape.
```
### Constraints & Assumptions
- `1 <= commentId <= 10^5`, and IDs are unique across all `addComment` and `addReply` calls.
- `1 <= author.length, content.length <= 100`
- `parentId` in `addReply` always refers to an existing comment, which may already be deleted.
- `getComment`, `deleteComment` and `getReplyChain` may be called with IDs that do not exist.
- At most `10^4` operations in total.
- Siblings, including top-level comments, are ordered by insertion time.
### Clarifying Questions to Ask
- Should deleted placeholders still appear in `getAllComments` and `getReplyChain`, including a deleted comment that has no replies?
- How should siblings be ordered in the traversal: insertion order, ID order, or something like score?
- How deep can a single thread get? That decides whether a recursive traversal is safe.
- Is the class used from a single thread, or does it need to handle concurrent callers?
### What a Strong Answer Covers
- A node representation and index that make the single-ID operations constant time, with links in both directions between parent and replies.
- Soft deletion that keeps the node in place, returns the right boolean for repeated and unknown IDs, and keeps replies reachable.
- Building the reply chain from the target to its root and returning it root first.
- A pre-order traversal with correct depth values and a deterministic sibling order that does not fail on very deep threads.
- Time and space complexity for each method.
### Follow-up Questions
- How would you support hard deletion, pruning placeholders that no longer have any replies?
- How would you page through a thread with thousands of replies under one comment?
- How would you order siblings by score when votes keep changing after a comment is posted?
- How would you store this model in a relational database and fetch a whole thread or a reply chain efficiently?
Overview: Design a class that stores nested comments, accepts replies, soft-deletes comments while keeping their replies, and returns reply chains and a depth-first listing with nesting depth. It tests tree modeling with parent and child links, constant-time lookup by id, and traversal that still works on very deep threads.