Find Path Between Fibonacci Tree Nodes
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to reason about recursive tree structures, map preorder indices to positions in a recursively defined Fibonacci tree, and design space-efficient algorithms for large inputs.
Constraints
- 1 <= k <= 60
- 1 <= a, b <= size(k), where size(1) = 1, size(2) = 1, and size(i) = 1 + size(i - 1) + size(i - 2)
- Do not construct the full tree explicitly; solve using subtree sizes and preorder index ranges
Examples
Input: (1, 1, 1)
Expected Output: [1]
Explanation: T(1) has only one node, so the path from 1 to 1 is just [1].
Input: (3, 2, 3)
Expected Output: [2, 1, 3]
Explanation: In T(3), node 2 is the left child of root 1 and node 3 is the right child, so the path goes through the root.
Input: (5, 5, 6)
Expected Output: [5, 3, 2, 6]
Explanation: In T(5), node 5 lies under node 3, while node 6 is the right child of node 2. Their lowest common ancestor is node 2.
Input: (6, 7, 10)
Expected Output: [7, 3, 2, 8, 10]
Explanation: Both nodes are inside the left subtree of the root. The path climbs from 7 to 2, then descends to 10.
Input: (6, 6, 13)
Expected Output: [6, 4, 3, 2, 1, 11, 12, 13]
Explanation: Node 6 is deep in the left side of T(6), while node 13 is in the right subtree of the root, so the path passes through node 1.
Input: (5, 7, 7)
Expected Output: [7]
Explanation: When a and b are the same node, the path contains only that node.
Hints
- For a subtree T(m) whose root has preorder index s, its left subtree occupies indices [s + 1, s + size(m - 1)] and its right subtree starts at s + 1 + size(m - 1).
- Find the root-to-a path and the root-to-b path using only subtree ranges, then combine them at their lowest common ancestor.