Mark a Deleted Forest Subtree
Updated
Problem statement
A forest of nodes numbered from 0 through n - 1 is represented by parent. Every root points to itself, and every nonroot entry is the index of its parent.
Delete the subtree rooted at deleteIndex by returning a copy of parent in which that node and every descendant have value -1. Preserve every entry outside the deleted subtree exactly.
Function
markDeletedSubtree(parent: int[], deleteIndex: int) → int[]Examples
Example 1
parent = [0,0,0,2,4,4]deleteIndex = 2return = [0,0,-1,-1,4,4]Nodes 2 and 3 are the selected subtree and become -1.
Example 2
parent = [0,0,1,1]deleteIndex = 3return = [0,0,1,-1]Only the selected leaf is marked.
Example 3
parent = [0,0,2,2,4]deleteIndex = 2return = [0,0,-1,-1,4]The other rooted trees retain their original parent entries.
Constraints
1 <= parent.length <= 200000.0 <= parent[i] < parent.length.- The parent links form a valid forest whose only cycles are self-pointing roots.
0 <= deleteIndex < parent.length.
More Pinterest Phone Screen questions
- Shortest Pin Path Through TopicsPHONE SCREEN · Seen Nov 2026
- Top K Pins with Insert and QueryPHONE SCREEN · Seen Nov 2026
- Pins Violation Log QueriesPHONE SCREEN · Seen Nov 2026
- Sparse Matrix Addition and MultiplicationPHONE SCREEN · Seen Nov 2026
- Minimum Bus Routes to a DestinationPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delete and Compact a Forest SubtreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Optimal Account BalancingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Aggregate Matching IPv4 Addresses Across ResponsesPHONE SCREEN · Seen Oct 2026
- Escape Room LeaderboardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Shortest Way to Form a Target StringPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Validate a Tree From Its Parent ArrayGoogle · PHONE SCREENGoogle interview questions
- Detonate the Maximum BombsZomato / Eternal · PHONE SCREENZomato / Eternal interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
- Aggregate Machine TopologyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Deterministic Depth-First Graph TraversalWaymo · PHONE SCREENWaymo interview questions
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
- All Nodes Distance K in a Binary TreeAmazon · PHONE SCREEN
- Detonate Bombs with Chain ReactionsAirbnb · PHONE SCREENAirbnb interview questions
- Report Island Areas in Discovery OrderSnap Inc. · PHONE SCREENSnap Inc. interview questions