FastPrepPart 1: The Narrowest Key
Problems/Part 1: The Narrowest Key

Part 1: The Narrowest Key

Scale AI logoScale AI● MediumFULLTIMENEW GRADINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The reports share the same lowest-common-ancestor task, and our version is about 98% the same as the reported questions. The Narrowest Key report gives the key-delegation story, the parent-array input, an example, and a follow-up about reusing ancestor chains across queries. We keep the existing child-list method, node 0 as the root, and the existing limits. We also explain how to convert the parent array. Keeping each full ancestor chain in a cache can take quadratic space on a deep tree. A later report describes a similar question in a Scale AI online assessment. It gave no different rules, so our version stays the same.

Special thanks: 🌷 A special thank you to a lovely, amazing fren for sharing that a similar question appeared on October 26, 2026! ૮₍ ˃ ⤙ ˂ ₎ა 🧡

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) → int

Examples

Example 1

children = [[1,2,3],[4,5],[],[6],[],[],[]]p = 4q = 5return = 1

Nodes 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 = 0

Node 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 = 3

Node 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 = 1

The 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 = 0

The 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 = 4

The 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 node 0 is the root.
  • Every node other than 0 appears 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

Interview reports that mention this question

  • Scale AI OA: What It Tests (2026)

Scale AI interview experiences

  • Scale AI Forward Deployed Engineer Debugging Round: 1 Experience
  • Scale AI ML Research Engineering Intern Interviews: 1 Experience
  • Scale AI SWE Intern Debugging Interview: 9 Experiences
  • Scale AI New Grad SWE and MLE Question Bank
  • Scale AI New Grad SWE Poker Screen and Onsite: 1 Experience
All Scale AI interview questionsSee Scale AI hiring insights
Saved
class Solution {
    public int lowestCommonAncestor(int[][] children, int p, int q) {
        // Write your solution here
    }
}
Checking account…