Lowest Common Ancestor with Parent Pointers
Updated
Problem statement
A rooted tree is stored in an integer array parent. For each node i, parent[i] is the index of its parent. The root has no parent, so its entry is -1.
You are given two node indices, first and second. Return their lowest common ancestor. This is the deepest node that lies on both paths from the two nodes up to the root.
The tree is always valid, and both indices are valid nodes. Use O(1) auxiliary space, meaning only a fixed amount of extra memory. Follow parent links to find the depth of each node. Move the deeper node up until the two depths match. Then move both nodes up together, one step at a time, until they meet.
Function
lowestCommonAncestorWithParents(parent: int[], first: int, second: int) → intExamples
Example 1
parent = [-1,0,0,1,1,2,2]first = 3second = 4return = 1Nodes 3 and 4 are both direct children of node 1.
Example 2
parent = [-1,0,0,1,1,2,2]first = 3second = 6return = 0The two nodes sit in different branches below the root. The only node they share on the way up is the root, so their lowest common ancestor is node 0.
Constraints
1 <= parent.length <= 100000.parentdescribes one valid rooted tree: exactly one entry is-1, and following parent links from every other node reaches that root.0 <= first, second < parent.length.- The algorithm must use
O(1)auxiliary space.
More Adobe interview questions
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Lowest Common Ancestor Only When Both Targets ExistONSITE INTERVIEW · Seen Aug 2026
- Next and Previous Lexicographic PermutationONSITE INTERVIEW · Seen Aug 2026
- Asteroid CollisionONSITE INTERVIEW · Seen Aug 2026
- Farthest Affordable Shop per QueryONSITE INTERVIEW · Seen Aug 2026
- Minimum Removals for a Nice ArrayONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Trie: Add and Search WordsONSITE INTERVIEW · Seen Jul 2026
- Base Unit ConversionsOA · Seen Nov 2026
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Two Tree Values That Add Up to a TargetExpedia · ONSITE INTERVIEWExpedia interview questions
- Lowest Common Ancestor with Parent PointersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- First Common Ancestor with Parent PointersMeta · PHONE SCREENMeta interview questions
- Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
- Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions
- Reverse a String Keeping Dashes in PlaceNykaa · ONSITE INTERVIEWNykaa interview questions
- Merge Sorted ArrayDP World · ONSITE INTERVIEW
- Detect a Loop in a Linked ListEPAM · ONSITE INTERVIEWEPAM interview questions
- Decode a Bit String With a Code TreeGoogle · ONSITE INTERVIEWGoogle interview questions
- Remove Duplicates From Sorted Array In PlaceKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
Adobe interview experiences
- Adobe New Grad OA: Unit Conversion and Code Review: 1 Experience
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe New Grad MTS Interview Questions: 7 Experiences
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)