Microsoft Interview Question for Software Engineer in Tests


Country: India




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

node reverse(node head,int n)
  {
    if(head==null)
    return null;
     node p,q=null,temp=head;
     while(k--)
     {
        p=q;
        q=temp;
        temp=temp.next;
        q.next=p;
      }
     head.next=reverse(temp,k);
   return q;
}

- techcoder October 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice soltion.

- anon October 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what is "K" here?

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

wht is K????

- Anonymous November 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess above sol need some modifications bt nice thought :)


node reverse(node head,int n)
{
int k = n; // need to initialize k
node p,q=null,temp=head;
if(head==null)
return null;
while(k--)
{
p=q;
q=temp;
temp=temp.next;
q.next=p;
}
head.next=reverse(temp,n); // pass n instead of n
return q;
}

- Anonymous November 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i dnt knw if m interpreting it correctly or not....but it's written "no new node can be allocated"

plz smone clarify!
Thnx!

- codez July 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

use recursion . in each recursion revrese N nodes

node reverse(node head,int n)
{
if(head==null)
return null;
node p,q=null,temp=haed;
while(k--)
{
p=q;
q=temp;
temp=temp.next;
q.next=p;
}
head.next=reverse(temp,k);
return Q;
}

- techcoder October 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry to give formaating
use recursion . in each recursion revrese N nodes

node reverse(node head,int n)
{
if(head==null)
return null;
node p,q=null,temp=haed;

while(k--)
{
p=q;
q=temp;
temp=temp.next;
q.next=p;
}
head.next=reverse(temp,k);
return Q;
}

- techcoder October 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we are not allowed to allocate new nodes here.

- anon October 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if we just interchange the data and not the nodes? Since we cannot allocate new nodes, we can't swap the nodes themselves.

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

How about this one without recursive method?

void reverseLinkedListbyInterval(NODE **head,int n)
{
NODE *curNode, *swapNode,*prevNode=NULL,*reverseList=NULL,*newHead=NULL,*tempNode;

curNode = *head;

int counter =n;
if (n >= 1)
{
while(curNode)
{
while (counter>0)
{
tempNode = curNode->pNext;
curNode->pNext = reverseList;
reverseList = curNode;
curNode = tempNode;
counter--;
}
counter=n;
if (newHead==NULL)
newHead=reverseList;
else
{
for(swapNode = newHead;swapNode->pNext;swapNode = swapNode->pNext);
swapNode->pNext = reverseList;
}
reverseList=NULL;
}
}
*head = newHead;
}

- N.M October 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another version of the same

//Reverse alternate nodes
NODE *reverseAlternateNodes(NODE *head, int number)
{

if(number < 2)
return head;
NODE *prevListTail = NULL, *curNode = head, *newHead = NULL;
while(curNode)
{
NODE *prevNode = NULL, *nextNode = NULL, *sub_list_tail = NULL, *sub_list_head = NULL;
int count = number;
while(curNode && count)
{
if(number == count)
sub_list_tail = curNode;
//swap the node
nextNode = curNode->pNext;
curNode->pNext = prevNode;
prevNode = curNode;
curNode = nextNode;
--count;
if(curNode == NULL)
sub_list_head = prevNode;
else if(count == 1)
sub_list_head = curNode;
}
if(prevListTail)
prevListTail->pNext = sub_list_head;
else
newHead = sub_list_head;

prevListTail = sub_list_tail;
}
return newHead;
}

- N.M October 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse ( struct node *first , int n )
{
struct node *cur, *temp;
int i, j;
cur = first;
while ( cur) {
temp = cur->prev;
cur->prev = cur->next;
cur->next = temp;
cur = cur->prev;
} while ( cur != NULL);
cur = first;
i = 0 ;
while ( cur) {
j = 0;
while ( j < i) {
cur = cur ->prev;
j++;
}
j = i + ( 2 *n) -1;
temp = cur;
while ( j > 0 ) {
temp = temp->prev;
j--;
}
if ( !temp)
break;
cur->next = temp;
temp->prev = cur;
}
cur = first;
for ( i = 1; i<n; i++)
cur = cur->prev;
first = cur;
first->prev = NULL;
}

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

i just tried it by using a doubly linked list.. please tell me if i am wrong somewhere.. i am a beginner.. please oblige

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

R(k, n)
	r = NULL
	if(k && tn)
		tn = n->nx
		n->nx = r
		r=n
		n = tn
		k--
	return r
	
PR(n)
	if(n == NULL)
		return
	h = n
	r = R(n, k)
	h->n = PR(tn)
	return r

- Prateek Caire November 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The code is nice. It seems to have a bug or constraint that the length of linklist n and the interval k must have a GCD bigger than 1. For example, I test n=10, k=3, the program has an error. I fix the code as the following:

LinkNode* linkedlist::Reverse_Interval(LinkNode* head, int n){
	if(!head) return NULL;
	LinkNode* p, *q=NULL, *temp=head;
	int k=n;
	while(k--){
		p=q;
		q=temp;
		temp=temp->next;
		if(!temp) break;
		q->next=p;
	}
	head->next=Reverse_Interval(temp,n);
	return q;
}

Input: {75 191 176 175 155 151 143 128 118 98}
Output: {176 191 75 151 155 175 118 128 143 98}
here n=10, k=3

- geliang2008 December 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The code works on an assumption that the linked list contains no cycle.

- geliang2008 December 04, 2011 | 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