Microsoft Interview Question






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

Write a function to maere tow sorted linded list

- Zinash Getahun January 31, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You already have one sorted linked list A, so just add each node of the 2nd linked list(B) into its proper place. Here's a typical alg to do this...

You have the heads/tails, use those as indicators. For example, choose node 1 from A. If node 1A is greater than node 1B and less than tailB, then place it in the proper position. If node1A is not in this range, it must be greater than tailB, so append it to tailB. Do this until A is empty.

- Jack January 31, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Oops. Meant place nodes from A into B.

- Jack January 31, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi, Jack, your algorithm will have to scan linked list B for every element in A. The runing time 'd be O( n * m). We need only O(n+m). Just keep one anchor pointer for A , another for B. Move the node with smaller value to linked list C and ajust the anchor pointer. It takes O(n+m) and no additional space.

- Anonym April 15, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Missing: check of the list orders
if opppsite order (small to large and large to small) reverse one of the lists

- Eila June 11, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use the merge sort principle for merging two sorted linked lists.

- Anonymous August 01, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I liked this one

16struct jsw_node *jsw_merge ( struct jsw_node *a, struct jsw_node *b )
17...{
18 struct jsw_node result, *it = &result;
19 struct jsw_node **half;
20
21 while ( a != NULL && b != NULL ) ...{
22 half = ( a->data < b->data ) ? &a : &b;
23 it->next = *half;
24 it = *half;
25 *half = (*half)->next;
26 }
27
28 it->next = ( a == NULL ) ? b : a;
29
30 return result.next;
31}

see at httpdot//wwwdoteternallyconfuzzleddotcom/tuts/algorithms/jsw_tut_sortingdotaspx

- Jack of All October 13, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code for merging two sorted Link list without using any additional memory..

#include<stdio.h>
#include<stdlib.h>
struct node
{
	int value;
	node * next;
};

void merge(node * , node *);
void main()
{
	int i, n1=0,n2=0,val = 0;
	node *temp, *l1, *l2, *prev;
	printf("Enter the no. of nodes in the first link list\n");
	scanf("%d", &n1);
	printf("Enter the values of the link list in order\n");
	scanf("%d", &val);
	l1 = (node *) malloc(sizeof(node));
	l1->value = val;
	l1->next = NULL;
	prev= l1;
	for(i=1;i<n1;i++)
	{
		scanf("%d", &val);
		temp = (node *) malloc(sizeof(node));
		temp->value = val;
		temp->next = NULL;
		prev->next = temp;
		prev = temp;		
	}

	printf("Enter the no. of nodes in the second link list\n");
	scanf("%d", &n2);
	printf("Enter the values of the link list in order\n");
	scanf("%d", &val);
	l2 = (node *) malloc(sizeof(node));
	l2->value = val;
	l2->next = NULL;
	prev = l2;
	for(i=1;i<n2;i++)
	{
		scanf("%d", &val);
		node * temp = (node *) malloc(sizeof(node));
		temp->value = val;		
		temp->next = NULL;
		prev->next = temp;
		prev = temp;
	}
	merge(l1,l2);
}
void merge(node * l1, node * l2)
{
	int i;
	node * head;
	node * prev, * temp;
	if(l1 == 0 || l2 == 0)
		return;
	if(l1->value < l2->value)
	{
		head = l1;
		l1 = l1->next;
	}
	else
	{
		head=l2;
		l2 = l2->next;
	}
	prev = head;

	while(l1 != NULL && l2 != NULL)
	{
		if(l1->value < l2->value)
		{
			prev->next = l1;
			prev = l1;
			l1 = l1->next;
		}
		else
		{
			prev->next = l2;
			prev = l2;
			l2 = l2->next;
		}			
	}//End of while
	if(l1==NULL)
		prev->next = l2;
	else
		prev->next = l1;

	printf("The new sorted link list is:\n");
	temp = head;
	while(temp != NULL)
	{
		printf("%d ",temp->value);
		temp = temp->next;
	}
}

- gauravk.18 February 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The two lists are not in sorted order.
I think you will give a in-place merge sort algorithm, what a shame

- Anonymous June 18, 2008 | 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