Part 1: The Narrowest Key
Updated
Problem statement
A secure network has devices arranged in a rooted general tree. A device’s key unlocks that device and every device in its subtree. Given two target devices, return the owner of the narrowest-scoped key that unlocks both: their lowest common ancestor.
Devices are labeled from 0 through n - 1. In this practice interface, node 0 is the root and children[i] contains every direct child of device i. Given labels p and q, return their lowest common ancestor. A device is an ancestor of itself.
The Narrowest Key source uses a parent array delegatedBy, with -1 for the root. To use this child-list interface, append each non-root device i to children[delegatedBy[i]]. The judged call remains lowestCommonAncestor(children, p, q).
Follow-up: repeated queries may reuse preprocessing. Explain how cached ancestor chains avoid rebuilding paths, what their worst-case space costs, and how a more compact index can support many queries. The executable task still answers one two-device query.
For batches of requests with two or more devices, continue to Part 2: Batch Access Review.
Function
lowestCommonAncestor(children: int[][], p: int, q: int) → intExamples
Example 1
children = [[1,2,3],[4,5],[],[6],[],[],[]]p = 4q = 5return = 1Nodes 4 and 5 are both direct children of node 1, so their lowest common ancestor is 1.
Example 2
children = [[1,2,3],[4,5],[],[6],[],[],[]]p = 4q = 6return = 0Node 4 lies below child 1, while node 6 lies below child 3. Their paths first meet at the root, node 0.
Example 3
children = [[1,2,3],[4,5],[],[6],[],[],[]]p = 3q = 6return = 3Node 3 is an ancestor of node 6 and of itself, so it is the lowest common ancestor.
Example 4
children = [[1,2],[3,4],[5,6],[],[7],[],[],[]]p = 3q = 7return = 1The source parent array is [-1,0,0,1,1,2,2,4]. It converts to the child lists above. Device 3 has root path 0 → 1 → 3; device 7 has root path 0 → 1 → 4 → 7. Their deepest shared device is 1. Keys 0 and 1 unlock both targets, and key 1 covers the narrower subtree.
Example 5
children = [[1,2],[3,4],[5,6],[],[],[],[]]p = 3q = 6return = 0The source parent array [-1,0,0,1,1,2,2] converts to these child lists. Device 3 has root path 0 → 1 → 3; device 6 has root path 0 → 2 → 6. Their deepest shared device is the root, 0.
Example 6
children = [[1,2],[3,4],[5,6],[],[7],[],[],[]]p = 4q = 7return = 4The source parent array [-1,0,0,1,1,2,2,4] converts to these child lists. Device 7 has root path 0 → 1 → 4 → 7. Device 4 is an ancestor of both itself and device 7, so the narrowest shared key belongs to device 4.
Constraints
1 <= n = children.length <= 100000.- The node labels are exactly
0, 1, ..., n - 1, and node0is the root. - Every node other than
0appears exactly once across all child lists, every listed label is valid, and all nodes are reachable from the root. 0 <= p, q < n. The targets may be equal.
More Scale AI Online Assessment questions
- Part 2: Batch Access ReviewOA · Seen Nov 2026
- Build Sessions from Unordered Events (Part 1)OA · Seen Oct 2026
- Match Billing Charges to User Sessions (Part 2)OA · Seen Oct 2026
- Four-Player Trick-Taking Card Game SimulationOA · PHONE SCREEN · Seen Aug 2026
- Free Windows Between Busy IntervalsOA · PHONE SCREEN · Seen Apr 2026
- Compare Labeling Worker AnnotationsOA · Seen Mar 2025
- Count Stable Server SubsegmentsOA · Seen Nov 2024
- Distinct OR Values of Increasing SubsequencesOA · Seen Nov 2024
- Running Through Walls with Limited EnergyOA · Seen Sep 2024
- Relative Score Graph ComponentsOA · Seen Sep 2023
Similar questions at other companies
- Base Unit ConversionsAdobe · OAAdobe interview questions
- Maximum Data Transfer TimeMicrosoft · OAMicrosoft interview questions
- Longest Transfer Time in a Server TreeRippling · OARippling interview questions
- Maximum Reservoir PowerWells Fargo · OAWells Fargo interview questions
- Minimum Leaf Path Sum over All RootsInfosys · OAInfosys interview questions
- Sum Dynamic Weighted Tree DistancesJuspay · OA
- Special Diameter EndpointsAgoda · OAAgoda interview questions
- Change Segment ColorsTrend Micro · OATrend Micro interview questions
- Tree Node With Maximum BeautyC3.ai · OAC3.ai interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions