First Common Ancestor with Parent Pointers
Updated
Problem statement
A rooted tree is represented by an integer array parent. Nodes are numbered from 0 through parent.length - 1; parent[i] is the parent of node i, and the single root has parent -1. Child pointers are implied by the same tree.
Given node indices first and second, return their first common ancestor when walking upward from the two nodes. A node is considered an ancestor of itself, so if one selected node is an ancestor of the other, return that selected node. The two selected nodes may be equal.
Use O(1) auxiliary space. You may determine each depth by following parent links, lift the deeper node until both depths match, and then move both nodes upward until they meet.
Function
firstCommonAncestor(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 siblings whose nearest common ancestor is node 1.
Example 2
parent = [-1,0,0,1,1,2,2]first = 1second = 4return = 1Node 1 is an ancestor of node 4 and counts as its own ancestor.
Example 3
parent = [-1,0,0,1,1,2,2]first = 3second = 6return = 0The nodes lie in different child subtrees of the root, so their first common ancestor is node 0.
Constraints
1 <= parent.length <= 100000.parentdescribes exactly one valid rooted tree: 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 Meta Phone Screen questions
- Fuzzy Word Lookup With One Misspelled LetterPHONE SCREEN · Seen Nov 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- First Element Greater Than KPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- All Elements of Two BSTs in OrderAdobe · PHONE SCREENAdobe interview questions
- Two Tree Values That Add Up to a TargetExpedia · ONSITE INTERVIEWExpedia interview questions
- Lowest Common Ancestor with Parent PointersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Merge Two Binary TreesSnowflake · PHONE SCREENSnowflake interview questions
- Merge Two Sorted ListsLinkedIn · PHONE SCREENLinkedIn interview questions
- Check if a String Is a PalindromeCisco · PHONE SCREENCisco interview questions
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Reverse a StringAmazon · PHONE SCREEN
- Compact Conversation History by Token BudgetSierra · PHONE SCREENSierra interview questions
- Remove Duplicates from Sorted ArrayNykaa · PHONE SCREENNykaa interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences