Facebook/Meta | Phone Screen | LCA 3

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2022-02-23Added By Anonymous
Source: Link

Company: Meta
Role: e3, e4
Interview Stage: Phone Screen
Location: London
Year: unknown

Original Post Content:

Recently did my phone screen for London location. Below two questions were asked-

1) https://leetcode.com/problems/buildings-with-an-ocean-view/
2) https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-iii/

I solved the second question with O(N) space which can be done in O(1). I realised it after solving the problem. Hence got rejected. :(

Adding my solution for reference:

public Node LCA(Node a, Node b) {
Set<Integer> nodes = new HashSet<>();
while (a != null) {
nodes.add(a.val);
a = a.parent;
}
while (b != null) {
if (nodes.contains(b.val)) return b;
b = b.parent;
}
return b;
}

Comments