Reddit: Hide a comment and its whole subtree when it matches an unwanted topic
Problem. You receive a dictionary of comments. Each comment has an id, a parent_comment link, a body, and two flags, cat and dog, marking what it discusses. The parent links make the comments a tree.
There are two viewer modes: a cat person who does not want dog comments, and a dog person who does not want cat comments. If a comment is about the unwanted animal, it must be hidden along with every reply underneath it.
Implement get_comments_to_exclude(comments, mode) returning the set of ids to hide.
Example. If comment 5 is about dogs and its replies form the chain 5 -> 6 -> 7 -> 8, then for the cat-person mode the answer is {5, 6, 7, 8}.
Follow-up. State the time and space complexity of your approach.
Notes. Input is given as child-to-parent links, so build a parent-to-children map first and then traverse down from each flagged comment. This came from an onsite for a machine-learning engineering role; the same loop's design round was about predicting comment likelihood.
Comments 0
No comments yet. Ask the author something, or add what happened in your own round.