FastPrepFilter Undesired Comments and Their Descendants
Problems/Filter Undesired Comments and Their Descendants

Filter Undesired Comments and Their Descendants

Reddit logoReddit● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: One interviewer asked: "There is a pile of comments, and each comment has its own parent comment. Each comment is dog or cat. A dog person hides all cat comments, including their child comments, and a cat person does the reverse. Output the IDs to exclude." The interviewer also made clear that for a dog person the check is only !isCat, not isDog && !isCat. The follow-up was: "If a comment's score is above some threshold, it should still be shown."

Another report says: "Given some Reddit post JSON. Part 1: filter comments by the user's preference. A cat person removes the dog comments and all their subtrees. Part 2: each comment has points. If the subtree sum is over the threshold, keep it. Note that you have to find the subtree sum starting from every dog comment."

The reports do not say how comments are given or what "above" means at the exact threshold. Here, comments are arrays indexed by ID, a neutral label marks a comment that is neither cat nor dog, and a comment stays only when its total is strictly greater than the threshold. One report says the comment's score and the other says the subtree sum. This version uses the subtree sum: the comment's own points plus all its descendants' points, counted over the whole original subtree. Every undesired comment is checked, even one inside a comment that stays. Points are whole numbers from 0 to 10000, and the answer is sorted by ID. A threshold of 1000000000 gives Part 1.

The judged task matches the reports at about 85%.

Comments are numbered by array index. parent[i] is -1 when comment i starts a thread, or the ID of the comment it replies to. animal[i] is cat, dog, or neutral, and points[i] is the comment's points.

A user whose preference is cat does not want dog comments, and a user who prefers dog does not want cat comments. These are the undesired comments. A neutral comment is never undesired, and neither is a comment with the preferred label.

The subtree total of a comment is its own points plus the points of all its descendants. An undesired comment stays if its subtree total is greater than threshold. Otherwise it is removed, and removing a comment also hides all of its descendants, whatever their labels.

Check every undesired comment, including one inside the subtree of an undesired comment that stays. A subtree total always counts the whole original subtree, even the parts that end up hidden.

Return the IDs of all hidden comments in ascending order.

In the first part of the interview there was no points rule, and every undesired comment was removed. A threshold of 1000000000 gives that behavior, because no subtree total can be greater than it.

Function

undesiredCommentIds(parent: int[], animal: String[], preference: String, points: int[], threshold: int) → int[]

Examples

Example 1

parent = [-1,0,0,1,1]animal = ["neutral","dog","cat","cat","neutral"]preference = "cat"points = [5,1,2,3,4]threshold = 1000000000return = [1,3,4]

The user prefers cats, so dog comments are undesired. Comment 1 is the only dog comment. Its subtree is comments 1, 3 and 4, with a total of 1 + 3 + 4 = 8 points, which is not greater than 1000000000. So comment 1 is removed and comments 3 and 4 are hidden with it, whatever their labels.

Example 2

parent = [-1,0,0]animal = ["dog","cat","neutral"]preference = "dog"points = [0,0,0]threshold = 0return = [1]

The user prefers dogs, so only the cat comment, comment 1, is undesired. Its total is 0, which is not greater than 0, so it is removed. The neutral comment 2 stays: a neutral comment is never undesired.

Example 3

parent = [-1,0,1,1,3,0]animal = ["neutral","dog","neutral","dog","cat","dog"]preference = "cat"points = [1,4,2,1,3,6]threshold = 6return = [3,4,5]

The dog comments are 1, 3 and 5. Comment 1 has a total of 4 + 2 + 1 + 3 = 10 points, which is greater than 6, so it stays. Comment 3 sits inside comment 1's subtree but is still checked on its own: its total is 1 + 3 = 4, so comments 3 and 4 are hidden. Comment 5 has a total of exactly 6, which is not greater than 6, so it is hidden too.

Example 4

parent = [3,-1,1,-1]animal = ["neutral","cat","neutral","cat"]preference = "dog"points = [2,5,0,1]threshold = 4return = [0,3]

Parents can have larger IDs than their children, and there can be several roots. Comment 1 is a cat comment with a total of 5 + 0 = 5, which is greater than 4, so it stays. Comment 3 is a cat comment with a total of 1 + 2 = 3, so it is removed and its child, comment 0, is hidden with it. The answer is sorted by ID.

Constraints

  • 0 <= n <= 10000, where n is the length of parent, animal, and points.
  • parent[i] is -1 or an ID from 0 to n - 1 other than i, and following parents from any comment always reaches -1. There can be several threads, a parent can have a larger ID than its child, and one thread can be a single chain of n comments.
  • animal[i] is cat, dog, or neutral. preference is cat or dog.
  • 0 <= points[i] <= 10000 and 0 <= threshold <= 1000000000, so every subtree total fits in a 32-bit integer.

More Reddit problems

  • Billing Log with Undo and RedoPHONE SCREEN · Seen Nov 2026
  • Tennis Game and Set ScoringPHONE SCREEN · Seen Oct 2026
  • Straight-Line Word SearchPHONE SCREEN · Seen Sep 2026
All Reddit interview questionsSee Reddit hiring insights
Saved
class Solution {
    public int[] undesiredCommentIds(int[] parent, String[] animal, String preference, int[] points, int threshold) {
      // write your code here
    }
}
Checking account…