KatInterview

Hide Flagged Comment Threads for a Pet-Preference Reader

Trees & GraphsAuto-graded · 28 test cases
MediumAuto-gradedTrees & Graphstreedfshash-map

Last updated

Find all comments hidden from a reader when any flagged comment hides its whole reply subtree, handling unordered input, dangling parent IDs, and very deep chains.

A discussion thread is stored as a flat list of comment records. Each comment is either a reply to another comment (its parent) or a top-level comment, so together the comments form a forest of reply trees. Every comment also has two boolean flags telling whether it talks about cats and whether it talks about dogs.

A reader browses in one of two modes:

  • "CAT_PERSON": the reader wants to avoid anything about dogs.
  • "DOG_PERSON": the reader wants to avoid anything about cats.

When a comment mentions the animal the reader wants to avoid, that comment is hidden together with every reply beneath it, at any depth. Your task is to return the IDs of all hidden comments.

Input

comments is a list of dictionaries with these keys:

KeyTypeMeaning
idintUnique comment ID
parent_commentint or NoneID of the comment being replied to, or None if top-level
bodystrComment text
catboolThe comment is about cats
dogboolThe comment is about dogs

The list may be in any order. A reply can appear before the comment it replies to.

mode is a string, either "CAT_PERSON" or "DOG_PERSON".

Rules

  • In "CAT_PERSON" mode, a comment is unwanted if its dog flag is True. In "DOG_PERSON" mode, a comment is unwanted if its cat flag is True. A comment with both flags set is unwanted in both modes.
  • A comment is hidden if it is unwanted, or if any ancestor (parent, grandparent, and so on) is unwanted. Once an ancestor is unwanted, the comment's own flags are irrelevant.
  • If parent_comment holds an ID that is not present in comments, treat that comment as top-level. The missing parent never causes a comment to be hidden.
  • The body text has no influence on the result.
  • Return the hidden IDs sorted in ascending order, with no duplicates. If nothing is hidden, return [].

Constraints

  • 0 <= len(comments) <= 100000
  • IDs are distinct integers with 0 <= id <= 10^9.
  • parent_comment is None or an integer in the same range; it never equals the comment's own id, and following parent links never forms a cycle.
  • A chain of replies can be as long as the total number of comments.
  • mode is exactly "CAT_PERSON" or "DOG_PERSON".

Examples

Example 1

Input: find_hidden_comments([{'id': 0, 'parent_comment': None, 'body': 'Look! A cute baby elephant taking a nap!', 'cat': False, 'dog': False}, {'id': 2, 'parent_comment': 1, 'body': 'I agree!', 'cat': False, 'dog': False}, {'id': 11, 'parent_comment': 0, 'body': 'Almost as cute as my poodle!', 'cat': False, 'dog': True}], 'CAT_PERSON')

Output: [11]

Example 2

Input: find_hidden_comments([{'id': 5, 'parent_comment': None, 'body': 'My puppy learned to sit today', 'cat': False, 'dog': True}, {'id': 6, 'parent_comment': 5, 'body': 'Congratulations!', 'cat': False, 'dog': False}, {'id': 7, 'parent_comment': 6, 'body': 'Thanks!', 'cat': False, 'dog': False}, {'id': 8, 'parent_comment': 7, 'body': 'Any training tips?', 'cat': False, 'dog': False}, {'id': 9, 'parent_comment': None, 'body': 'My cat ignores every command', 'cat': True, 'dog': False}], 'CAT_PERSON')

Output: [5, 6, 7, 8]

Example 3

Input: find_hidden_comments([{'id': 6, 'parent_comment': 5, 'body': 'Same here', 'cat': False, 'dog': False}, {'id': 3, 'parent_comment': 2, 'body': 'My dog does that too', 'cat': False, 'dog': True}, {'id': 1, 'parent_comment': None, 'body': 'What do your pets do all day?', 'cat': False, 'dog': False}, {'id': 5, 'parent_comment': 4, 'body': 'My cat and my dog nap together', 'cat': True, 'dog': True}, {'id': 2, 'parent_comment': 1, 'body': 'My cat sleeps on the keyboard', 'cat': True, 'dog': False}, {'id': 4, 'parent_comment': 1, 'body': 'Mostly sleeping, I assume', 'cat': False, 'dog': False}], 'DOG_PERSON')

Output: [2, 3, 5, 6]

Hints

Hint 1

Hiding spreads from a parent to its replies, so think of the result as the union of whole subtrees rooted at each unwanted comment.

Hint 2

Building a map from parent ID to the list of child IDs lets you walk downward from unwanted comments instead of repeatedly climbing up ancestor chains, which would be quadratic on a long chain.

Hint 3

Only register a child under a parent that actually exists in the input, and use an explicit stack rather than recursion because the reply chain can be 100,000 deep.