Find cheapest common ancestor in a tree
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates understanding of tree data structures and ancestor concepts (such as lowest common ancestor) along with algorithmic skills for aggregating node attributes like minimum values, situating it in the Coding & Algorithms domain and testing practical application backed by conceptual understanding of ancestor relationships.
Constraints
- 1 <= n == len(parent) == len(prices) <= 10^5
- The input forms a valid rooted tree with exactly one root
- -10^9 <= prices[i] <= 10^9
- 0 <= a, b < n
- a and b belong to the same tree and may be equal
Examples
Input: ([-1, 0, 0, 1, 1, 2, 2], [10, 5, 8, 7, 2, 6, 1], 3, 4)
Expected Output: 1
Explanation: The common ancestors of nodes 3 and 4 are 1 and 0. Their prices are 5 and 10, so node 1 is the cheapest.
Input: ([-1, 0, 0, 1, 1, 2, 2], [10, 5, 8, 7, 2, 6, 1], 5, 6)