Yahoo Interview Question Software Engineer / Developers




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

.Question didn't ask to swap alternate pairs instead it is asking for swapping alternate elements. For example: 1-->2-->3-->4-->5 should be transformed to 3-->4-->5-->2-->1.

public static LinkedListNode swapAlternateNodes(final LinkedListNode head) {
    if (head == null || head.next == null) {
        return null;
    }

    LinkedListNode newhead = null;
    LinkedListNode prev = head;
    LinkedListNode cur = prev.next;
    LinkedListNode temp = null;
    while (cur != null && cur.next != null) {
        temp = cur.next;

        prev.next = cur.next.next;
        cur.next = prev;
        temp.next = cur;

        if (newhead == null) {
            newhead = temp;
        } else if (newhead.next == prev) {
            newhead.next = temp;
        } else if (newhead.next.next == prev) {
            newhead.next.next = temp;
        }

        prev = cur;
        cur = cur.next;
    }

    return newhead;
}

- zahidbuet106 on June 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 6 vote

Working code in C++ :-

node* swap_alt(node *root)
{
	if(root == NULL) return NULL;	
	if(root->next == NULL) return root;
	node *cur,*nxt,*tmp,*ret;
	cur=root;
	tmp=NULL;
	while(cur != NULL && cur->next != NULL)
	{
		nxt=cur->next;
		cur->next=nxt->next;
		nxt->next=cur;
		if(tmp == NULL)
		{
			ret = nxt;
			tmp = cur;
		}
		else
		{
			tmp->next = nxt;
			tmp = cur;
		}
		cur=cur->next;
	}
	return ret;
	
}

- helper on August 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

wrong solution. Question didn't ask to swap alternate pairs instead it is asking for swapping alternate elements. For example: 1-->2-->3-->4-->5 should be transformed to 3-->4-->5-->2-->1.

public static LinkedListNode swapAlternateNodes(final LinkedListNode head) {
    if (head == null || head.next == null) {
        return null;
    }

    LinkedListNode newhead = null;
    LinkedListNode prev = head;
    LinkedListNode cur = prev.next;
    LinkedListNode temp = null;
    while (cur != null && cur.next != null) {
        temp = cur.next;

        prev.next = cur.next.next;
        cur.next = prev;
        temp.next = cur;

        if (newhead == null) {
            newhead = temp;
        } else if (newhead.next == prev) {
            newhead.next = temp;
        } else if (newhead.next.next == prev) {
            newhead.next.next = temp;
        }

        prev = cur;
        cur = cur.next;
    }

    return newhead;
}

- zahidbuet106 on May 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Hi vinodjayachandran, does these questions are asked in Yahoo, Bangalore??

- Anonymous on June 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yep @ Bangalore yahoo

- Vinod Jayachandran on June 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

p=head;s=NULL;
while( p!=NULL && p->next !=NULL && p->next->next !=NULL)
{
q=p->next;
r=q->next;

p->next=r->next; // swap p and r
r->next=q;
q->next=p;

if(s!=NULL)
s->next=r; // connect prev of p to r
s=r; // set r for next iteration
p=q; // for next iteration
}

- Random Guy on June 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your if(S!=NULL) loop will never run.

- TestDoc on June 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry. My bad

- TestDoc on June 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i'm still confused...... when will your ****if(s!==NULL)**** run??

- Anonymous on June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

One general doubt: Does swapping the nodes in such questions means changing the links? Or data swapping is also permissible?

- Akash on June 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think swapping of content would be the final thing the interviewer would accept. If you make this attempt, interviewer may say, "ok, how about just pointer swapping, in place?".

- AnonymousBD on June 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

LinkList* swapAlternateNodes(LinkList *ptr)
{
	if(ptr == NULL || ptr->next == NULL)
		return ptr;

	LinkList dummy, *p, *q;
	dummy.next = ptr;
	p = &dummy;
	q = p->next;

	while(q != NULL && q->next != NULL)
	{
		p->next = q->next;
		q->next = p->next->next;
		p->next->next = q;
		p = q;
		q = p->next;
	}
	return dummy.next;
}

- Anonymous on July 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Hum, why thinking so hard? Why don't just swap the data value instead of next pointer?

- chau nguyen on April 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

i think according to question
1->2->3->4->5->6->7->8 will be converted to 3->4->1->2->7->8->5->6

now we can find symmetry that if we treat 2 nodes as a single node (1->2 = A, 3->4 = B)
than the problem will be converted to just swapping consecutive node..... just extra precaution in special case if nodes are less than 2 or nodes are in odd number.... and these precaution will be taken in any solution.....

- arpit on June 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

please let me know if i'm correct .....

- arpit on June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think, it's: 2->1->4->3->6->5

- newfoundpassion on June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

arpit , i think u have a weird mind and not capable of being in software industry.

- google on July 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ above "google" u can keep your thinking to yourself..

- google on September 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

//Swap data only

void List::swapAlternateData()
{
    Node* n1 = root();
    
    if(!n1)
        return;
    
    Node* n2 = n1->next();
    
    while (n2) {
    
        int data = n1->data();
        n1->setData(n2->data());
        n2->setData(data);
        
        if (!n2->next())
            break;
        
        n1 = n2->next();
        n2 = n1->next();
    }
}

//Swap actual nodes

void List::swapAlternateNode()
{
    Node* n1 = root();
    
    if(!n1)
        return;
    
    Node* n2 = n1->next();
    Node* n3 = root();
    while (n2) {
        Node* temp = n2->next();
        n1->setNext(temp);
        n2->setNext(n1);
        
        if (n3 == root())
           setRoot(n2);
        else 
           n3->setNext(n2);
           
        if (!temp)
            break;
        
        n3 = n1; 
        n1 = temp;
        n2 = n1->next();
    }
    
}

- Anonymous on June 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

hey how did you applied for yahoo@bangalore..thru referral ?

- thear on June 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

void reverse_list()
{
if(head==NULL || head->next==NULL)
return;

mynode *prev, *cur, *temp1;

cur=head;

while(cur!=NULL && cur->next!=NULL)
{
temp1 = cur->next;
cur->next = cur->next->next;
temp1->next = cur;
if(cur!=head)
{
prev->next = temp1;

}
else
head=temp1;
prev = cur;
cur = cur->next;
}
}

- Kunal on September 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

node * swap(node *p)
{
    if(p==NULL||p->next==NULL) return p;
    node *q=swap(p->next->next);
    node *r=p->next;
    r->next=p;
    p->next=q;
    return r;
}

- Anonymous on September 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code is for swapping two consecutive nodes which can be easily modified for this problem as follows

node * swap(node *p)
{
    if(p==NULL||p->next==NULL || p->next->next==NULL || p->next->next->next==NULL) return p;
    node *q=swap(p->next->next->next->next);
    node *r=p->next->next;
    r->next->next=p;
    p->next->next=q;
    return r;
}

- Anonymous on December 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

if somebody is looking for code in java, then this is the answer:

public class AlterLinkedList{
    private static void printLinkedList(Node head){
        for(Node curNode=head; curNode!=null; curNode=curNode.next)
            System.out.print(curNode.val+" ");
        System.out.println();
    }
    private static Node alterLinkedList(Node head){
        Node curNode=head;
        Node next1=curNode.next;
        if(next1==null) return curNode;
        curNode.next=next1.next;
        next1.next=curNode;
        head=next1;
        Node next2;
        while(curNode!=null){
            next1=curNode.next;
            if(next1==null) break;
            next2=next1.next;
            if(next2==null) break;
            curNode.next=next2;
            next1.next=next2.next;
            next2.next=next1;
            curNode=next1;
        }
        return head;
    }
    public static void main(String[] argv){
        Node head=new Node(1);
        Node curNode=head;
        for(int i=2; i<=1; i++){
            curNode.next=new Node(i);
            curNode=curNode.next;
        }
        printLinkedList(head);
        head=alterLinkedList(head);
        printLinkedList(head);
    }
}
class Node{
    int val;
    Node next;
    public Node(int val){
        this.val=val;
        this.next=null;
    }
}

- wolverine on December 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

#include<stdio.h>
#include<conio.h>
struct node
{
struct node *next;
int data;
}*front,*rear;
void insert(int ele)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node *));
temp->data=ele;
temp->next=NULL;
if(front==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->next=temp;
rear=temp;
}
}
void display()
{
struct node *temp;
if(front==NULL)
printf("\nempty list");
else
{
printf("\n");
temp=(struct node *)malloc(sizeof(struct node *));
for(temp=front;temp!=NULL;temp=temp->next)
printf("%d",temp->data);
}
}
void swap()
{
int t;
if(front==NULL)
printf("empty list");
else if(front->next==NULL)
printf("single element");
else
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node *));
temp=front;
while(temp!=NULL&&temp->next!=NULL)
{
t=temp->data;
temp->data=temp->next->data;
temp->next->data=t;
temp=temp->next->next;
}
}
}
main()
{
int ch,ele;
clrscr();
while(1)
{
printf("\n1.insert\n2.display\n3.swap\n4.exit");
printf("\nenter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nenter any element");
       scanf("%d",&ele);
insert(ele);
break;
case 2:display();
break;
case 3:swap();
break;
case 4:exit(0);
break;
}
}
}

- Anonymous on December 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

if(n == null)
return n;
SList head = swap(n);
SList tail = head;

while(tail == null || tail.next != null){
tail.next.next = swap(tail.next.next);
tail = tail.next.next;
}

return head;

}

private static SList swap(SList n){

if(n.next == null || n==null){
return n;
}
SList current = n.next;
SList next = current.next;
n.next = next;
current.next = n;
return current;

}

- Parth on January 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

public boolean swapTwoConsecutiveNodes(){
		if(head == null) {
			System.out.println("List is empty");
			return false;
		}
		Node prev = null;
		Node next = null;
		Node start = head;
		
		head = start.nextNode;
		while(start != null && start.nextNode != null){
			next = start.nextNode;
			if(prev != null) prev.nextNode = next;
			start.nextNode = next.nextNode;
			next.nextNode = start;
			prev = start;
			start = start.nextNode;
		}
		return true;
	}

- Abhishek Kothari on June 02, 2014 | Flag Reply


Add a Comment
Name:

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

Books

is a comprehensive book walking you through 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