Microsoft Interview Question for SDE-3s


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 vote

public Node lca(Node node,int data1,int data2){
		if(node==null )
			return null;
		
		if(node.nData==data1 || node.nData==data2)
			 return node;
		
		Node left = lca(node.leftNode, data1, data2);
		Node right = lca(node.rightNode, data1, data2);
		
		if(left!=null && right!=null)
			return node;
		else{
			if(left==null)
				return right;
			else
				return left;
		}
	}

- akhil.gupta@imaginea.com January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How does approach take O(log(n)) time if tree is not balanced?

- EPavlova January 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Edited the question. Thanks, good catch.

- william.brandon.lee83 January 18, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

x

- Anonymous January 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone point directions how to do with iterative approach in O(1)?

- popeye123 January 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Farah-Colton Bender algorithm is for the generic case where given a tree (a K-tree, not necessarily Binary), we have to make "repeated" queries about the LCA of any two arbitrary nodes.
Hence a preprocessing step is kind of mandatory (just like in Tries or Suffix Trees)
For this question, can we not solve it by finding the "point of convergence" of two linked list ? In this case it is a point of divergence, rather !

- Debabrata Pani July 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Provided an iterative solution with O(1) space complexity.
Assumed the tree is mutable.

The idea is to do the in-order traversal with O(1) space complexity
using the Morris traversal technique.
Note that the LCA node is the node at the minimum depth
among nodes visited after finding the first one.

BTNode* findLCAIterative(BTNode* node, int data1, int data2)
{
  bool found = false;
  BTNode *ret = nullptr;
  int minDepth = numeric_limits<int>::max();
  int depth = 0;

  while(node){
    if(node->left){
      // check whether we visited all of left-side decendents
      BTNode *tmp = node->left;
      int tmpDepth = 0;
      while(tmp->right && tmp->right != node){
        tmp = tmp->right;
        tmpDepth++;
      }

      if(!tmp->right){
        // we haven't visted any of left-side decendents
        // create back link for the return
        // and visit left child
        tmp->right = node;
        node = node->left;
        depth++;
        continue;
      }
      // we have visited all of left decendents
      // recover the pointer
      tmp->right = nullptr;
      // update the depth
      // considering one link for left, another for the back link
      depth -= tmpDepth + 2;

      // we recovered all back links
      if(found && depth == 0)
        break;
      // set LCA to current node if needed
      if(!found && ret && depth < minDepth) {
        ret = node;
        minDepth = depth;
      }
    }

    // visit current
    if(!found && (node->data == data1 || node->data == data2)){
      if(ret) {
        // we found the LCA
        // but we need to finish this traversal
        // to recover temporary links
        found = true;
      }
      else {
        // found the first one
        ret = node;
        minDepth = depth;
      }
    }

    node = node->right;
    depth++;
  }

  return found ? ret : nullptr;
}

- linuxchain October 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Simple recursive tree iteration algorithm requires O(n), where n is a number of nodes in the tree.

Iterative algorithm requires O(n) for calculating parents of the nodes (if no pointers to parent inside node structure are available) and O(h) running time, where h is tree height. If tree is balanced h = log N, hence O(log N).

One of the best solutions is Farah - Colton and Bender algorithm. It requires O(n) for preprocessing and O(1) for each online request. But it quiet complex.

We can also modify iterative algorithm so that it takes O(n log n) for preprocessing and O(log n) for lookup calculating each 2^k, k = 0, 1, ... parent for each node and using this information to speed up a lookup.

Recursive implementation (simple, but not effective):

struct node {
	struct node *left;
	struct node *right;
	char key; // For simplicity
};

struct node* lca(struct node *head, struct node *n1, struct node *n2) {
	if (!head || !n1 || !n2) { return nullptr; }

	if (head->key == n1->key || head->key == n2->key) { return head; }
	struct node *p1 = lca(head->left, n1, n2);
	struct node *p2 = lca(head->right, n1, n2);
	if (p1 && p2) { return head; }
	return p1 ? p1 : p2;
}

- J.J. Carpenter January 18, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More