Yahoo Interview Question for Software Engineer / Developers






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

Use two pointers to traverse the link list, the first one move 1 step, second one move 2 steps, when the second pointer reach the end, set the head of the second list to be the next of the first pointer (and also remove the link from the previous pointer)

public node* SplitList(node* head)
{
if(head == null)
{
return;
}
node* p1 = head;
node* p2 = head;

while(p2 != null)
{
p2 = p2->next;
if(p2!=null)
{
p2 = p2->next;
}
p1 = p1->next;
}
node* head2 = p1->next;
p1->next = null;
return head2;
}

- Nat September 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution fails when no of nodes are even. check it once

- Anonymous September 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The solution does not fail. The first pointer always points to the last element of the first list.
Check it with 4 or 6. They both work. With even 2 elements.
In case of two we need to switch the first and second lists because when there are 2 elements, when the loop ends pointer 1 points to 2 second and pointer 2 points to the first element.

- Anonymous September 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Nat :- check for n=3

- Aditya October 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take 2 pointers, move one pointer 2 nodes and other 1 node

In case of 4 nodes
Pointer 1 => pointing to 2 node
Pointer 2 => pointing to 4th node



In case of 5 nodes
Pointer 1 => pointing to 3rd node
Pointer 2 => pointing to NULL.

Point pointer 2 to pointer 1->next

- DashDash September 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes I think this will work 

p1 = head;
p2 = head->next;

while( p2!= NULL || p2->next!=NULL )
{    p2 = p2->next;
     if( p2!=NULL) p2 = p2->next;
     
     p1 = p1->next;

}

front = head;
last = p1->next;
p1->next = NULL;

- Aditya October 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

slow=fast=head;
while(fast->next!=NULL && fast->next->next!=NULL)
{
     slow=slow->next;

- Anonymous October 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ignore it. i have submitted the full code after it.

- Mridul Malpani October 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

slow=fast=head;
while(fast->next!=NULL && fast->next->next!=NULL)
{
     slow=slow->next;
     fast=fast->next->next;
}
front=head;
if(slow->next!=NULL)
{
    last=slow->next;
}
else
{
    last=NULL;
}

- Anonymous October 20, 2010 | 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