Microsoft Interview Question for Software Engineer / Developers






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

I dont why people have written so huge codes
Here is a small piece of running code
At point of time the situation is like prev->a->b->something

Node swapAdjacentNodes(Node root)
{   Node prev=NULL;
    Node n=root;Node a,b;
    while(n!=NULL && n->next!=NULL)
    {   a=n;
        b=n->next;
        if(prev!=NULL)
        {   prev->next=b;
        }
        else
        {   root=b;
        }
        a->next=b->next;
        b->next=a;
        prev=a;
        n=a->next;

    }
    return root;
}

- ravikant0909 July 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is it like every linked list is of type : struct node {struct next, int data}; ok?

Iterate through the list : Find the pair
-----------------------------------
| a | a->next| b | b->next | .......
------------------------------------

a->next = b->next->next
b->next = a

Am I wrong?

- netappreject May 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Was this question asked at Microsoft DC recruting event..I was asked similar and I provided similar answer.
Eg: Two pointer where second pointer is ahead by 1 place
Increment both the pointers by 2 to find pair od adjacent nodes

I could not complete the code though...

- curiosity May 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's not good to use a method only applied for this question. e.g. what if the interviewer want you to swap every 3 adjacent nodes instead of 2? like a->b->c->d->e->f => c->b->a->f->e->d ? I think use recursive function could solve this problem. Can someone give the code?

- tangqiyuan May 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Nodeptr ReverseN(Nodeptr head, int number) {
 
    if(number < 2) return head;
    Nodeptr pre_sub_list_tail = NULL, curr_ptr = head, reversed_list_head = NULL;
    while(curr_ptr) {
           Nodeptr prev_ptr = NULL, nxt_ptr = NULL, sub_list_tail = NULL, sub_list_head = NULL;
           int count = number;
           while(curr_ptr && count) {
               if(number == count) sub_list_tail = curr_ptr;
               nxt_ptr = curr_ptr->nxtptr;
               curr_ptr->nxtptr = prev_ptr;
               prev_ptr = curr_ptr;
               curr_ptr = nxt_ptr;
               --count;
               if(curr_ptr == NULL) sub_list_head = prev_ptr;
               else if(count == 1) sub_list_head = curr_ptr;
           }
           if(pre_sub_list_tail) {
               pre_sub_list_tail->nxtptr = sub_list_head;
           }else {
               reversed_list_head = sub_list_head;
           }
           pre_sub_list_tail = sub_list_tail;
    }
    return reversed_list_head;
 
}

- Erik May 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void CustomInvert(node* a)
{
	node* tmp = a;
        node* prev = NULL;
	while(tmp != NULL && tmp->next != NULL)
	{
		Swap(tmp, tmp->next, prev);
                prev = tmp;
		if(NULL != tmp->next)
		{
			tmp = tmp->next;
		}
	}
}

void Swap(node* a, node* b, node* prevA)
{
	if(NULL == a || NULL == b)
	{
		// log exception();
		return;
	}

    a->next = b->next;
    b->next = a;
    if(NULL != prevA)
    {
	    prevA->next = b;
    }

}

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

Aligned it now:

void CustomInvert(node* a)
{
    node* tmp = a;
    node* prev = NULL;
    while(tmp != NULL && tmp->next != NULL)
    {
        Swap(tmp, tmp->next, prev);
        prev = tmp;
        //tmp = tmp->next;
        if(NULL != tmp->next)
        {
            tmp = tmp->next;
        }
    }
}

void Swap(node* a, node* b, node* prevA)
{
    if(NULL == a || NULL == b)
    {
        // log exception();
        return;
    }

    a->next = b->next;
    b->next = a;
    if(NULL != prevA)
    {
        prevA->next = b;
    }

}

- Pranay May 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node* ReverseAdjLists(Node *root)
{
Node *head = root,*curr1= NULL, *curr2= NULL,*next = NULL;
head = root->ptr;
curr1 = root;
curr2 = root->ptr->ptr;
while(curr2 != NULL || curr1->ptr != NULL)
{
next = curr1->ptr;
if(curr2 == NULL)
curr1->ptr = NULL;
else
curr1->ptr = curr2->ptr;
next->ptr = curr1;
if(curr2 == NULL)
curr1->ptr = NULL;
else
curr1 = curr2->ptr->ptr;
if(curr2 != NULL)
{
next = curr2->ptr;
curr2->ptr= curr1->ptr;
next->ptr = curr2;
curr2 = curr1->ptr->ptr;
}
}
return head;

- anuMicrosoft May 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a special case of the "every k element reverse problem"
The Original problem was:-
Reverse every k element in the lisk list.
I/P: 1->2->3->4->5->6->7->8->NULL
k=2
O/P: 2->1->4->3->6->5->8->7->NULL

I/P: 1->2->3->4->5->6->7->8->NULL
k=3
O/P: 3->2->1->6->5->4->7->8->NULL

I/P: 1->2->3->4->5->6->7->8->NULL
k=4
O/P: 4->3->2->1->8->7->6->5->NULL

The similar soln can be found

goursaha.freeoda.com/DataStructure/KElementReverseInLinkList.html

- Anonymous May 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

MS IDC makes fool of people.
People have to come to office on weekends due to workload and do night outs, no work life balance. They pay 10-20% more make people labour.

Do take the feedback from employees before joining MS.

And work is junk, all junk wor from Redmond is transferred to IDC. Ask any team, whether they design, implement products or just do porting or maintenance or make tools.

- Anonymous June 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

tested and worked fine (recursive):
Node<int>* swapCoupleNodes(Node<int>* node)
{
if ( !node )
return NULL;
else if ( node->next == NULL)
return node;

else
{
Node<int>* temp = node->next->next;
Node<int>* ret = node->next;
node->next->next = node;
node->next = swapCoupleNodes(temp);
return ret;
}

}

- sniffer June 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void swap (node *list)
{
node *temp = list->next; // temporary variable
list->next=list->next->next;
list->next->next=list;
list=temp;
if(list->next != NULL)
swap(list->next->next);
}

- raghavendra August 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1.linked list is given pointed by head
2.temp1=head,temp2=temp1->next;
while(temp2)
{
swap(temp1,temp2);
temp1=temp1->next;
temp2=temp1->next;
}
swap(temp1,temp2)
{
temp3=temp1;
temp1=temp2;
temp2=temp3;
}

- ramu kumar September 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

MAIN HOON BOND!!!!!!!!!!!

- bond !!!!!! September 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

main bhi hoon superbond

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

Can anyone please explain the logic on how to do this problem?

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

I have come up with a recursive solution and i found this to be more intuitive

linkedlist changeAlternate(linkedlist head)
	{	
		
		if(head==null||head.next==null)
		return head; 
			
		linkedlist temp2=head.next;
		linkedlist temp = head.next.next;
		head.next.next=head;
		head.next=changeAlternate(temp);
		head=temp2;
		return temp2;
	}

- Vathul April 09, 2013 | 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