Bankbazaar Interview Question


Country: India
Interview Type: Phone Interview




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

The problem actually whats to find out the first common node in two single lists.
For both nodes, traverse down to the root to get their lengths, let's say the length of node 1 is len1, the length of node 2 is len2, and len1 < len2.
Let p and q point to node 1 and node 2 respectively, q moves forward len2-len1 steps first, then p and q both move step by step until they reach the same node, which is their lease common ancestor.

- chshda March 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Keep 2 stacks, s1 and s2.

For node 1, traverse down to the root while push Nodes onto the s1 until the root is on top of the stack.

For node 2, traverse down to the root while push Nodes onto the s2 until the root is on top of the stack

Now, pop from both stacks and compare ancestors. If the Nodes popped are the same, it is a common ancestor, so record this as the current ancestor. when it comes to nodes popped that are not the same, we have a reached a non-ancestor. Return the current ancestor to get the least one.

- Skor March 22, 2015 | 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