Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

// merge sort: returns a pointer to the head of a sorted list
list *merge_sort(list *head) {
    if(head == 0)
        return 0;
    list *p = head, *pp = p, *prev = 0
    if(p->next == 0)
        return head; // already sorted
    // find a split in the middle using two ptrs
    while(pp != 0 && pp->next != 0) {
        prev = p, p = p->next;
        pp = pp->next->next;
    }
    prev->next = 0; // cut the list in the middle
    list *h1 = merge_sort(p),
         *h2 = merge_sort(head);   // sort the two parts

    head = h1, prev = 0;
    // merge the list 'h2' into 'h1' inplace
    while(h2 != 0) {
        if(h1 != 0 && h2->val >= h1->val) {
            prev = h1;      // just iterater through 'h1' list
            h1 = h1->next;
        } else { // insert 'h2' before 'h1'
            list *t = h2->next; // save the 'next' pointer
            if(prev == 0) { // insert a new head
                h2->next = head;
                head = h2;
            } else {
                prev->next = h2;
                h2->next = h1;
            }
            prev = h2; h2 = t;
        }
    }
    return head;
}

- pavel.em October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hello How do u decide that the prev pointer has reached middle of linklist.

- Monty January 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

He said using merge sort, why u implement merge_sort by yourself?

- Gang February 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Monty, as pp is moving through the list at twice the speed of p, when the function breaks out of the first while loop due to pp or pp->next being null p will point to the middle of the list.

- Anonymous March 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void mergeSort() {
		this.sortedHead = split(getHead());
	}



private LinkListNode<E> split(LinkListNode<E> head) {
		if (head.getNext() == null || head == null)
			return head;

		LinkListNode<E> firstHalf = getMiddleElement(head);
		LinkListNode<E> secondHalf = firstHalf.getNext();
		firstHalf.setNext(null);
		head = merge(split(head), split(secondHalf));
		return head;
	}

	private LinkListNode<E> merge(LinkListNode<E> splitHead1,
			LinkListNode<E> splitHead2) {

		LinkListNode<E> dummyHead = new LinkListNode<E>();
		LinkListNode<E> lastPos = dummyHead;

		while (splitHead1 != null && splitHead2 != null) {
			if ((Integer) splitHead1.getData() <= (Integer) splitHead2
					.getData()) {
				lastPos.setNext(splitHead1);
				splitHead1.setPrev(lastPos);
				splitHead1 = splitHead1.getNext();
				lastPos = lastPos.getNext();
			} else {
				lastPos.setNext(splitHead2);
				splitHead2.setPrev(lastPos);
				splitHead2 = splitHead2.getNext();
				lastPos = lastPos.getNext();
				
			}
		}

//If any remaining elements in any of the two link lists ... 

		while (splitHead1 != null) {
			lastPos.setNext(splitHead1);
			splitHead1.setPrev(lastPos);
			splitHead1 = splitHead1.getNext();
			lastPos = lastPos.getNext();
			
		}

		while (splitHead2 != null) {
			lastPos.setNext(splitHead2);
			splitHead2.setPrev(lastPos);
			splitHead2 = splitHead2.getNext();
			lastPos = lastPos.getNext();
			
		}
		return dummyHead.getNext();
	}

	private LinkListNode<E> getMiddleElement(LinkListNode<E> head) {
		if (head == null)
			return null;
		if (head.getNext() == null)
			return head;

		int count = 0;
		LinkListNode<E> fastP = head, slowP = head;
		while (fastP.getNext() != null) {
			fastP = fastP.getNext();
			count++;
			if (count % 2 == 0)
				slowP = slowP.getNext();
		}
		return slowP;
	}

- Anonymous January 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I use a wrapper method to call split and to print .. the head node would be sortedHead ..

- Chris January 11, 2012 | 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