Lowest Common Ancestor Queries Across a Large Transform Hierarchy
Last updated: November 29, 2026
Quick Overview
A scene is a forest of transforms given as a parent array (-1 for roots, parents may appear after children). Preprocess it to answer many queries for the lowest common ancestor of two transforms and the number of hierarchy hops between them, returning -1 when they are in different root trees.
Unity
November 29, 20260
0
0 solved
A scene is a forest of transforms given as a parent array (-1 for roots, parents may appear after children). Preprocess it to answer many queries for the lowest common ancestor of two transforms and the number of hierarchy hops between them, returning -1 when they are in different root trees.
Unity scenes can hold hundreds of thousands of transforms, and tools such as reparenting, relative transform computation and selection grouping need fast ancestor queries. The interviewer is looking for binary lifting (or Euler tour plus RMQ), careful preprocessing order, forest handling and cycle detection on malformed input.
What the Interviewer Expects
- Build children lists and process nodes top-down regardless of array order
- Precompute depth, root and 2^k ancestors
- Lift the deeper node, then lift both together from the highest power
- Return -1 for nodes in different trees
- Detect cycles in the parent array
- State preprocessing and per-query complexity
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you compute the relative transform matrix between two nodes using the LCA?
- How would you support frequent reparenting without full rebuilds?
- Compare binary lifting with Euler tour plus sparse table for this workload
- How would you answer the queries offline with Tarjan's algorithm?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: parent[i] for n transforms, a forest. Then up to millions of lca(a, b) and distance(a, b) queries. Queries must be much faster than walking to the root, since chains can be thousands deep...
High-Level Design
Binary lifting: up[k][v] is the -th ancestor of v, or -1. Compute up[0] from the parent array, then up[k][v] = up[k-1][up[k-1][v]]. Depth and root id come from a BFS from all roots over chi...