Yahoo Interview Question for Software Engineer / Developers


Team: Yahoo Sports
Country: United States
Interview Type: In-Person




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

This is same as finding Least Common Ancestor for A and B.

- Karthik January 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

does you approach have O(P) as requested, or O(N) if tree is unbalanced?

- Anonymous January 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

Approach: It is stated that node has child and parent properties. We can make use of parent property.
1. Start traversing from both the nodes using parent pointers'
2. For any one node, start pushing the nodes traveled in a hashtable. For another node traversal, keep check the node is present or not in hashtable. If node is present the found which is LCA of two nodes.
Probably have to work out the details where one traversal finishes before the other one but that can be easilly handled in the code.

If we want to avoid using any extra memory, we can treat this two list with one merging point. Which can be easily solved in O(h).

- sumeet.kukkar January 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Karthik is nailed it -- LCA problem. Greatly simplified LCA problem because we have a parent link. As such all that is necessary is:

- get depth of A (logN) by walking up
- get depth of B (logN) by walking up
- take deeper one and level with another one
- walk in parallel until they meet

- DS January 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1 @Karthik

Yea, agree its a LCA problem (common problem).... As mentioned in question, i tried to provide a solution with O(P) complexity in place of O(logN).

- Ajeet January 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can achieve it using extra space O(P)
where P = size of path between A & B.
Algorithm:

1.Node a = A;
2.Node b = B;
3.Map<Node,Node> path = new HashMap<Node,Node>();
4.while(a.p != null && b.p != null && a != b){
	if(!path.contains(a)){
		path.put(a,a.p);
		a = a.p;
	}else if(!path.contains(b.p)){
		path.put(b.p,b);
		b = b.p;
	}else{
		return path;
	}
}
//Map maintains elements in insertion order.
5.return path;

- Ajeet January 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I do not think the above code will work. Can you please elaborate further ?
Please explain the following cases.
(i) B is a child of A
(ii) A and B are on the same side but one of the node is much deeper than the other.
It will be O(P) only when the nodes are on different sides of root.

Thanks a lot.

- Anonymous January 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this (please let me know if this seems sound)

N1 = A
N2 = B
Hash H{} = null
Pathfound = false
CommonParent = null;

while patfound = false do
  if N1.Parent is not null and N1.Parent exists in H then
    PathFound = true;
    CommonParent = N1.Parent
  end If
  H = H union N1.Parent

  if N2.Parent is not null and N2.Parent exists in H then
    PathFound = true;
    CommonParent = N2.Parent
  end If
  H = H union N2.Parent
end while

Pseudo Code Desc:

Copy Node A,B to N1 and N2
Create empty Hash H
PathFound
We assume parent of root is null

Explanation:

Path = Node1 to Common Ancestor + Node2 to Common ancestor
We walk upward from both child nodes simultaneously. We check Both parents in a Hash first, and if not found we add the parent. Since we use hash table, we can assume checking and adding both parents can be done in constant time.

Ideal Case: both nodes are at a distance of 1 from a common ancestor
Will befound out in 1 step
Worst Case: One is the root and other is farthest leaf, will take P steps
Other Avg cases: One is a close node, other is a deep seeded node, will take less than P steps as Common Ancestor will come before P steps or exactly P steps if one is parent of other

In the end we will have common ancestor node. We traverse from each node to common ancestor (total of P steps again and find the path by traversing one of the paths from node to ancestor, the other in reverse)

This takes : P steps for finding ancestor, P steps to construct path from both nodes to ancestor, and P steps to traverse path. So 3P steps or O(P) when simplified.

- Prasad February 21, 2014 | 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