Yahoo Interview Question for Software Engineer / Developers






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

- Push all the nodes into 2 different stacks.
- Then pop once from both stacks to check if the nodes are same,
- - if so continue popping till the nodes differ.
- - else there are no common nodes

- onion834 September 04, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

If there is a common node between two link lists, then their last nodes should be same. Just compare the last node of both the link lists.

- Anonymous December 26, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If there is a common code, then it shd look like a 'Y'. So first go thru both linked list to find their lengths in O(n). Get the difference between length, say d.
say, head1 and head2 are pointers to linkedlists
scan thru the list :
compare head1->value == (head2+d)->value

total is O(n) time

- genehacker September 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think it work because its not a string matching.What u think ?

- ohm September 09, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry genehacker..I was confused, u r right.

- ohm September 09, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Traverse LL1. At its end add a nodeZ.
Then traverse LL2. If there is a common node, then the nodeZ will be found at the end of LL2

- Adiva September 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@adiva
your idea is good.. but i need to traverse LL1 once and place the node.. then traverse LL2 and in case if the list merges, I will have to traverse thru them again. Isn't there any way where I can finish off the traversal and simultaneously check for common node??

- asdf September 04, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I like the idea with the stacks. :) But you can make it for O(n) memory I believe:
bool CommonNode(List* aList, List* bList)
{
List* a = aList, *b = bList;
while( (a != NULL) && (b != null) )
{
if(a == b)
return true;
a = a->pNext;
b = b->pNext;
}
return false;
}

A valid improvement of the algorithm will be if you make step equal to a = a->pNext->pNext. Or even more, but this requires more chechs to not going read from null.

- ProdigalSon September 06, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Think this will work only if the common node is at the same distance from both the heads.

Please correct me if I am wrong.

- Balaji February 28, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, Balaji is right.
My solution would be to place the nodes of any linked list in a hashtable, then when traversing thro the other, we can find the common node.

- Ranganath March 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

All we need to do is to first calculate len=len1-len2 (len1>len2, as discussed above). Point p1 and p2 to list1 (the longer one) and list2 respectively. Then step only p1 for "len" no. of nodes. After this we are guaranteed that p1 and p2 are equidistant from the common node. So now start stepping p1 and p2 simultaneously until p1==p2. This will the common node. This is a simple O(n) solution.

- mshah2 September 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use recursive we can make it with o(n) time, only one transverse needed.

int commomNode(list* p1, listp2) {
if (p1 == NULL && p2 == NULL)
return 0;
else if (p1 == NULL)
return commomNode(p1,p2->next);
else if (p2 == NULL)
return comonNode(p1->next,p2);
else
return commonNode(p1->next,p2->next);

if (p1 == p2)
return 1;
else
return 0;
}
}

- lensbo September 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the algorithm of finding differences in lengths will not work since the first node itself can be the common node.

- tilo1583 September 18, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if first node is common than both lists are same and hence common length is zero and the length difference still holds true

- Anonymous October 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@lensbo
it's entirely wrong!
it simply traverses till end...

- ssn September 30, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@lensbo
it's entirely wrong!
it simply traverses till end...

- ssn September 30, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if it is forwaded singly linked list, just get the last node of one of the LL and compare with each node of other.

Say L1 - A->B->C->D->E->F->G->H
L2 - K->L->M->G->H OR K->L->M->N->O->P

Take H node from L1 and compare with each node in L2.

- sabs October 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

only the last one is correct.

- Anonymous October 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If there is a common node between two link lists, then their last nodes should be same. Just compare the last node of both the nodes.

- Anonymous December 26, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I was thinking if we need to search the common element then can we do it faster than O(n*n) any ideas of a better complexity algorithm to find the common element?

- Anonymous March 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer given by Anonymous on December 26, 2008 is correct.
Just one flaw, what if the linked list is a circular list. The algorithm fails
eg First Linked List A->B->C->D->C
Second Linked List Z->Y->C->D->C
To solve this have another data member in each node. Set the data member to 1 when we visit for the first time during First Linked List traversal.
When we traverse the second linked list, check if any node has the data member set to 1.

- Avinash October 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good observation, but inevitably the interviewer will ask for a solution that is constant space (over and above that for the input lists) i.e. no marking of the nodes or usage of extra stacks and other data structures. ;)

- Bullocks December 29, 2009 | Flag


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