Find Defenders Against Every Hostile Monster
Updated
Problem statement
A nested monster hierarchy has been flattened into three parallel arrays. names[i] is monster i's unique name, hostile[i] says whether it is hostile, and parent[i] is the index of the monster that can directly defeat it. A root has parent -1.
Defeat is transitive: if monster A can defeat B and B can defeat C, then A can defeat C.
Return the names of every non-hostile monster that can defeat every hostile monster. Preserve input order in the result.
Function
findDefenders(names: String[], parent: int[], hostile: boolean[]) → String[]Examples
Example 1
names = ["dragon","griffin","orc","imp"]parent = [-1,0,0,2]hostile = [false,false,true,true]return = ["dragon"]Dragon can defeat both hostile monsters through its descendants. Griffin cannot, and hostile monsters are not eligible defenders.
Example 2
names = ["atlas","guardian","rat"]parent = [-1,0,1]hostile = [false,false,true]return = ["atlas","guardian"]Both Atlas and Guardian have Rat in their transitive can-defeat subtree.
Example 3
names = ["oak","wolf","elm","goblin"]parent = [-1,0,-1,2]hostile = [false,true,false,true]return = []The hostile monsters lie in different trees, so no monster can defeat both.
Constraints
1 <= names.length <= 200000.parent.length == hostile.length == names.length.- Names are unique non-empty lowercase strings.
- Each
parent[i]is-1or a valid different index. - The parent relations form a forest with no cycles.
- At least one monster is hostile.
More Confluent Onsite Interview questions
- Implement tail -nPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sudoku Board ValidationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sudoku Puzzle SolverPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sequential Phrase Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Bounded Combination Sum with Signed CandidatesONSITE INTERVIEW · Seen Oct 2026
- Detect a Silent SensorONSITE INTERVIEW · Seen Sep 2026
- Build an Inverted Document IndexONSITE INTERVIEW · Seen Sep 2026
- Combination Sum with Unlimited ReuseONSITE INTERVIEW · Seen Sep 2026
- Word Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Windowed Key-Value MapPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
Similar questions at other companies
- Delete and Compact a Forest SubtreePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Recursive Display SortFigma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Chain of CommandIMC Trading · OAIMC Trading interview questions
- Render a Stable Comment HierarchyBobyard · PHONE SCREEN
- Chain of CommandUber · OAUber interview questions
- Validate a Tree From Its Parent ArrayGoogle · PHONE SCREENGoogle interview questions
- Validate BST Node DescriptionsAirbnb · OAAirbnb interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions