Interview Question for Software Engineer Interns


Country: India




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

public static Node mergeSort(Node head){

        if(head == null || head.next==null) return head;

        Node mid = getMidNode(head);
        Node secondList = mid.next;
        mid.next = null;

        return merge(mergeSort(head),mergeSort(secondList));
    }

    public static Node merge(Node h1 ,Node h2){

        Node result ;
        if(h1==null) return h2;
        if(h2 == null) return h1;

        if (h1.data <= h2.data){ result = h1 ; result.next = merge(h1.next,h2); }
        else{
            result = h2 ;
            result.next = merge(h1,h2.next);
        }

        return result;

    }

    public static Node getMidNode(Node head){

        Node fast,slow;
        fast = slow = head;

        while (fast.next!=null && fast.next.next!=null) {

            fast = fast.next.next;
            slow = slow.next;
        }

        return slow;
    }

- abhay0609 December 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

time complexity = (nlogn)
Space complexity = constant
ignoring stack for recursion execution which is (log n)

- abhay0609 December 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

heapsort

- zr.roman December 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tried to do it using mergesort and I think we can O(1) space complexity using mergesort but was unable to do it. Can you guide me upon that?

- user December 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

no, no, merge sort has O(n) space complexity! (though time is still O(n log n)).
But heapsort is in-place, i.e. O(1) space, while time is again O(n log n).

- zr.roman December 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can u provide me some link where I can find code and nice explanation about it, preferably in C++?

- user December 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

firstly, you can refer to the article "heapsort" on wikipedia, and then if necessary follow some references and links.
Another great source is MIT's OpenCourseWare on youtube, and especially the lecture "Heaps and Heap Sort" (.../watch?v=B7hVxCmfPtM).

- zr.roman December 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I know heapsort. I can't connect it with the given question. So, plz help me how to use heapsort to sort linked list in O(1) space.

- user December 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ok, I will provide my solution, but a little later.
(by the way, on wikipedia the algorithm is described using list DS, exactly your case, you can try to construct the algorithm yourself.)

- zr.roman December 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

looks like, heapsort will need O(n^2) time to perform on singly linked list (because of linear access to elements), though space still remains O(1).
So, the best variant here is merge sort with O(n logn) time and O(logn) space.
Compromise is necessary.

- zr.roman December 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Merge sort is the solution. Since this is a linked list(not a array), we don't need O(N) space. We can simply change the pointers while merging them.

- koustav.adorable December 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

in-place merge sort has O(n log^2 n) time complexity.

- zr.roman December 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Merge sort is recursive algorithm and each recursive call puts another frame on the stack.Hence It requires O(log(n)) space.

- vivek December 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Mergesort on linked list is done in-place. (zr.roman wrong again!)

- gen-x-s December 28, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

QuickSort can be performed on a linked list the same way as on regular array.
Mergesort is not O(1) space, and heapsort requires random access to items in the array to perform the operations on the heap. A random access operation would take O(n) in a linked list, which would increase the run time to (n^2 log n). Quick sort does not require random access operations, and can be done by just keeping a couple of pointers to the current scan and partition locations.

- gen-y-s December 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Quick sort has O(n^2) time in worst case.

- zr.roman December 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

and O(n) or O(logn) space.

- zr.roman December 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be done using iterative merge-sort. Merging linked lists can be done in place (constant space requirement). And recursion is avoided in the iterative version of merge-sort, by doing a bottom-up merge-sort: first merge each pair of consecutive nodes, then merge each pair of 2 nodes, then each pair of 4 nodes, etc.

- gen-y-s December 28, 2015 | 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