GSLab Interview Question for Technical Architects


Country: India
Interview Type: In-Person




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

Bubble sort? Seriously?

You are right: merge sort is usually a good way to sort a linked list. I wouldn't work for this company.

- 010010.bin July 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given that the lists are Objects in java, there may be concerns about the amount of memory consumed by all the distinct LinkedLists. I suspect, however, that the memory consumed by distinct LinkedLists would be more on the order of O(log n) so it wouldn't be much of an issue.

public static void sort(LinkedList<Integer> list){
    if(list == null){
        throw new NullPointerException();
    }

    //base cases
    if(list.size() == 0 || list.size() == 1){
        return;
    }

    //divide the list and sort each half
    LinkedList<Integer> firstHalf = splitLinkedList(list);
    sort(firstHalf);
    sort(list);

    //recombine the halves
    return merge(list, firstHalf);
}

private static LinkedList<Integer> splitLinkedList(LinkedList<Integer> list){
    LinkedList<Integer> firstHalf = new LinkedList<Integer>();
    int halfSize = list.size() / 2;
    for(int i = 0; i <halfSize ; i++){
        firstHalf.add(list.removeFirst());
    }
    return firstHalf;
}

private static LinkedList<Integer> merge(LinkedList<Integer> list1, LinkedList<Integer> list2){
    LinkedList<Integer> results = new LinkedList<Integer>();
    while(!list1.isEmpty() && !list2.isEmpty()){
        if(list1.peek() < list2.peek()){
            results.add(list1.removeFirst();
        }
        else{
            results.add(list2.removeFirst());
        }
    }
    results.addAll(list1);
    results.addAll(list2);
    return results;
}

- zortlord July 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

May be he is talking about singular Link list in that have only header so in that condition only bubble sort is only way

- Anonymous July 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

He was right. In a List you do not have random access (operator[]). You could store a the pointers to each position in a vector but you will need O(n) space. Using bubble sort you use O(1) space, but temporal O(n^2).

So it is a trade-off between size and time efficiency

- Anonymous July 22, 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