Symantec
3.2 of 5 1,554 reviews
www.symantec.com Mountain View, CA 5000+ Employees

Symantec Software Engineer Interview Question

I interviewed in Culver City, CA and was asked:
"find lowest common ancestor of 2 nodes in a binary tree"
Tags: binary tree, lowest common ancestor
Add Tags [?]
Answer Flag Question

Part of a Software Engineer Interview Review - one of 315 Symantec Interview Reviews

Answers & Comments

0
of 0
votes
I tried a complicated recursive methods and got stuck. If there is no restriction, we actually could use an hash map to store tree nodes while tracing up the parent nodes of the given nodes. if a common node is found to be parent of the given 2 nodes then print it out
- Interview Candidate on Jul 10, 2011 Flag Response
0
of 0
votes
Node lowestCommonAncestor(Node root, Node a, Node b) {
  if (root.contains(a) && root.contains(b)) {
     if (root.left != null && root.left.contains(a) && root.left.contains(b))
         return lowestCommonAncestor(root.left, a, b);
     else if (root.right != null && root.right.contains(a) && root.right.contains(b))
         return lowestCommonAncestor(root.right, a, b);
     else return root;
  } else return null;
}

boolean contains(Node root, Node x) {
   if (root == null || x == null) return false; // not needed. Defensive coding
   if (root == x) return true;
   if (root.left != null and root.left.contains(x)) return true;
   if (root.right != null and root.right.contains(x)) return true;
   return false;
}
- Anonymous on Jun 28, 2012 Flag Response
1
of 1
vote
int lowestCommonAncestor(Node node, int value1, int value2){

                if(node == null)
                        return -1;

                if(value1 > node.data && value2 > node.data)
                        return lowestCommonAncestor(node.right, value1, value2);
                else if(value1 < node.data && value2 < node.data)
                        return lowestCommonAncestor(node.left, value1, value2);
                else
                        return node.data;

        }
- double'o on Oct 19, 2012 Flag Response

To comment on this question, Sign In with Facebook or Sign Up


Symantec – Why Work for Us?

​​What do Symantec employees have in common? We are passionate about what we do and contribute to something really important! We protect the world’s digital information and interactions. At Symantec, you will enjoy an… Full Overview

Provided by employer [?]

Tags are like keywords that help categorize interview questions that have something in common.

Glassdoor is your free inside look at Symantec interview questions and advice. All interview reviews posted anonymously by Symantec employees and interview candidates.