Filter Undesired Comments and Their Descendants
Reported by candidates from Reddit's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure this Reddit problem hinges on is an adjacency list built from the parent array. The Reddit OA, reported in September 2026, hands you a comment forest and asks you to flag every undesired comment plus everything below it. It's a tree traversal wearing a moderation costume. With up to 100000 comments, the build-then-walk approach is the only one that holds up. If you blank on the traversal mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and gives you the solution in real time.
The problem
Comments are numbered by array index. parent[i] is -1 for a root or the ID of comment i's parent. animal[i] is cat, dog, or neutral. A user whose preference is cat considers dog comments undesired, and vice versa. Return every undesired comment plus every descendant of any undesired comment, sorted by ID. Neutral comments are included only when they descend from a marked comment. Function undesiredCommentIds(parent: int[], animal: String[], preference: String) → int[] Examples Example 1 parent = [-1,0,0,1,1] animal = ["neutral","dog","cat","cat","neutral"] preference = "cat" return = [1,3,4] Comment 1 is undesired, so both of its descendants are included regardless of label. Example 2 parent = [-1,0,0] animal = ["dog","cat","neutral"] preference = "dog" return = [1] Only the cat-labeled child is undesired. Constraints 0 <= parent.length <= 100000; both arrays have equal length. The parent links form a forest. preference is cat or dog.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build children lists from the parent array, since parent[i] points up and you need to go down. Mark every comment whose animal is the opposite of the preference as a seed. Then run a BFS or iterative DFS from each seed, marking all descendants regardless of label. Collect marked IDs and return them sorted. A simpler trick: since you can walk up, you can also check for each node whether any ancestor is undesired, memoizing results. The pitfall is recursion depth. A forest with 100000 nodes can be a single chain, so recursive DFS can overflow the stack in some languages. Use an explicit stack or queue. Another pitfall is including neutral comments that aren't under a seed. Neutral alone never qualifies. Scanning IDs in order at the end gives sorted output for free, no sort needed. If the live problem trips you up, StealthCoder is the hedge that reads the screen and hands you working code.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Filter Undesired Comments and Their Descendants cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Reddit's OA.
Reddit reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Filter Undesired Comments and Their Descendants FAQ
What's the trick in the Reddit undesired comments problem?+
Convert the parent array into a children adjacency list, then traverse downward from every comment labeled with the opposite animal. Everything reached gets marked. Neutral comments only count if reached from a seed. Finish by scanning IDs in order.
How hard is this one really?+
Easy to medium. The logic is simple graph traversal. The difficulty is handling 100000 nodes safely, which means avoiding deep recursion, and reading the neutral rule carefully. If you've done any tree BFS, you're fine.
Do I need recursion or can I go iterative?+
Go iterative. The input is a forest and could be one long chain of 100000 comments, which can blow the call stack. Use a stack or queue with a visited or marked array. It's the same complexity and much safer.
Do I need to sort the output?+
Not if you're careful. Mark comments in a boolean array, then loop from 0 to n-1 and collect marked indexes. That produces sorted IDs in linear time, avoiding an extra sort step.
How do I prepare for this in 48 hours?+
Practice building adjacency lists from a parent array and running BFS over a forest. Test edge cases: empty input, all neutral, a single long chain, and multiple roots. Write it iteratively once so it's muscle memory.