Facebook/Meta | Phone Screen | LCA 3
Anonymous User
4070

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 (18)