Microsoft Interview Question for Software Engineer in Tests






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

reverse(node *p)
{
node *q,*x;
q=NULL:
for(i=0;1<linklength;i++)
{
x=p;
while(x->next !=p)
{
x=x->next;
}
printf("%d",x->info);
p=x;
}

- Umang akash February 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node* Reverse(struct node* current) {

struct node* result = NULL;
struct node* next;

while (current != NULL) {
next = current->next; // tricky: note the next node
current->next = result; // move the node onto the result
result = current;
current = next;
}

return result

}

- Anonymous February 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

There are three pointers rights no???
current, next and result

- tej February 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

3 is fine because the question states, at most 2 'extra' pointers.

- cirus February 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You do not need the result ptr.
Your function should take a reference to the head pointer, you should advance it during the operation and your tail will be your new head.
It is not asked to keep the original list and create a clone which is the reversed version. So you may simply modify the original list.

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

void reverse(node ** head)
{
    node * prev = NULL;
    while(*head)
    {
         node * next = (*head)->next;
         (*head)->next = prev;
         prev = *head;
         *head = next;
    }
    *head = prev;
}

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

2 answers rec and iterative. No null pointer check, you can add it.

node* reverseLinkedList(node* input){	
	node* temp=NULL;
	node* first=NULL;
	if(input->next!=NULL)
		temp=reverseLinkedList(input->next);	
	else		
		return input;	
	input->next->next=input;
	input->next=NULL;
	return temp;
}

node* reverseLinkedListIterative(node* input){
	node* result=NULL;	
	node* next=NULL;

	while(input!=NULL){
		next=input->next;
		input->next=result;
		result=input;
		input=next;
	}
	return result;
}

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

I checked this code is working fine ..
void reverserLinkedList(Node **head)
{
Node *prev=NULL,*tmp=NULL;
while(*head)
{
tmp = (*head)->next;
(*head)->next = prev;
prev = *head;
*head = tmp;
}
*head = prev;
}

- Gabbar March 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct code

- DannY September 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

list* reverse(list *l){
list*a=NULL, *b=l;
while(b){
swap(a,b->next);
swap(a,b);
}
return a;
}

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

public void reverse(Node node)
{
	Node pointer = node;
	Node node2 = null;
	while(pointer != null)
		{
			node2 = new Node(pointer.getData(),node2);
			pointer = pointer.next;
		}
}

So this will generate reverse of a linked list in one simple iteration.

- Avishek February 10, 2014 | 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