Interview Question


Country: United States




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

My two pointer approach is O(n) time and O(1) space, but requires some preprocessing. First we create a sentinel head pointing to the first node. Then we create another fake node pointing to the first node of the second half.

For example, 1->3->5->2->4->6 becomes head->1->3->5->fake->2->4->6.

Now let p1 = head and p2 = fake. In each iteration, we let n1 & n2 be the next nodes of p1 & p2, respectively. We compare the values of n1 & n2. If n1 <= n2, we simply increment p1. Otherwise, we try to rip n2 out and insert it between p1 and n1, and then again increment p1. So we are actually incrementing p1 in both cases.

The while loop ends when either p1 or p2 has reached the end. That happens when p1->next (n1) is same as p2, or when p2->next (n2) is null.

while(true) {
	n1 = p1->next;
	n2 = p2->next;
	if(n1 == p2 || n2 == null)
		break;
	if(n1 <= n2) {
		p1 = p1->next;
	} else {
		p2->next = n2->next;
		p1->next = n2;
		n2->next = n1;
		p1 = p1->next;
	}
}
p1->next = p2->next

- Sunny December 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use the two pointer approach. Keep one pointer at the beginning of the first half and one at the beginning of the second half. Compare the values of the two pointers and make the required insertions. O(n) time, O(1) space

- alex December 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

total complexity with above approach will be O(n^2) which is not advisable. This can be done for any list, we are not taking advantage of both are sorted.

- Anonymous December 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

two steps:
(1)find the boundary of the two independently sorted list.
(2)change the pointer field of the node whose value is smaller.
Following is C code:

Node* sort(Node* head)
{
    if(head == NULL) return head;

    //step 1: find the boundary of the two independently sorted list
    Node *p = head, *left = head, *right;
    while(p->next != NULL && p->value <= p->next->value){
        p = p->next;
    }
    if(p->next == NULL) return head;
    else{
        right = p->next;
        p->next = NULL;
    } 

    //step 2: change the pointer field of the node whose value is smaller
    head = left->value > right->value ? right : left;
    while(left != NULL && right != NULL){
        if(left->value > right->value){
            p = right->next;
            right->next = left;
            right = p;
        }
        else{
            p = left->next;
            left->next = right;
            left = p;
        }
    }

    return head;
}

- uuuouou December 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry for the above code. It should be like this:

Node* sort(Node* head)
{
    if(head == NULL) return head;

    //step 1: find the boundary of the two independently sorted list
    Node *p = head, *q;

    while(p->next != NULL && p->value <= p->next->value){
        p = p->next;
    }
    if(p->next == NULL) return head;
    else{
        q = p->next;
        p->next = NULL;
    } 

    //step 2: merge list
    if(head->value > q->value){
        p = q;
        q = head;
        head = p;
    }
    
    Node* t = head;
    for(p = p->next; p != NULL && q != NULL; t = t->next;){
        if(p->value > q->value){
            t->next = q;
            q = q->next;
        }
        else{
            t->next = p;
            p = p->next;
        }
    }
    if(p != NULL) t->next = p;
    else t->next = q;

    return head;
}

- uuuouou December 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about just exchange val between nodes, whereas keep the next ptr stable? that will make the code much easier.

- busycai December 16, 2013 | 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