Tree Ancestor Queries
Updated
Problem statement
You are given a rooted tree as directed parent-to-child edges and a list of queries.
For each query [x, y], determine whether node x is an ancestor of node y. A node is an ancestor of another node if it can reach that node by following one or more child edges.
Return an integer array where the ith value is 1 when the answer for the ith query is true, and 0 otherwise.
Function
answerAncestorQueries(edges: int[][], queries: int[][]) → int[]Examples
Example 1
edges = [[1,2],[1,3],[2,4],[2,5],[3,6],[5,7]]queries = [[1,6],[6,1],[2,7],[2,6]]return = [1,0,1,0]Node 1 is an ancestor of 6; node 6 is not an ancestor of 1; node 2 is an ancestor of 7; and node 2 is not an ancestor of 6.
Constraints
1 <= edges.length + 1 <= 10^51 <= queries.length <= 10^5- The input edges form a rooted tree.
More Arcesium interview questions
- Ordered Payload ReleaseONSITE INTERVIEW · Seen Jul 2026
- Decode Numeric StringONSITE INTERVIEW · Seen Jun 2026
- City Infection NumberOA · Seen Jul 2025
- Minimum Tunnel Crossing TimeOA · Seen Jul 2025
- Product of Subset MaximaOA · Seen Jul 2025
- Reconstruct the Root StreamOA · Seen Jul 2025
- Add DigitsOA · Seen Aug 2023
- Binary Tree Maximum Path SumOA · Seen Aug 2023
- Detect a Cycle in Directed Hate RelationshipsOA · Seen Aug 2023
- Reverse First K Characters in Every 2K BlockOA · Seen Aug 2023
Similar questions at other companies
- Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Find Leaves of a Binary TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
- JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEWMongoDB interview questions
- Delete and Compact a Forest SubtreePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Onion-Peel Layers of a Binary TreeeBay · ONSITE INTERVIEWeBay interview questions