Groupon Interview Question for Software Engineer / Developers


Country: United States




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

Since the values are in a tight range you probably want to do either of :

1) If the items come with auxillary data and you want "stability" (which you might since the range is so small), use linear counting sort.

2) If you are just getting numbers then use bucket sort (tally counts).

---Optimization of either idea above----
Now optimize either idea above by stripping out the last sort-like step of both sorts if you care (that is you just grab the top k elements either from the prefix sums of counting sort or the first few buckets which have enough for k elements in bucket/tally sort).

- S O U N D W A V E November 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

If extra space is not an issue, then the following saves on time by performing O(n log k) comparison, on cost of O(k) additional space.

priority_queue<int,vector<int>,greater<int>>heap;
for(i=0;i<k;i++)
  heap.push(a[i]);
for(i=k;i<n;i++)
{
	if(a[i]>heap.top()) 
	{
		heap.pop();
		heap.push(a[i]);
	}
}

If we don't want to utilize any additional space, we can use any in-place sorting algorithm such as quick-sort to sort and select the first k elements. This takes O(n log n).

- Anonymous November 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

MAX HEAP:---

public class Node {

    private int data;

    public Node(int data) {
        this.data = data;
    }
}
/*
    * Insert @end of heapArray and trickle up until its greater than its parent.
    * */
    @Override
    public boolean insert(int key) {
        if (currentSize == maxSize) {
            return false;
        }
        Node node = new Node(key);
        heapArray[currentSize] = node;
        trickleUp(currentSize);
        currentSize = currentSize + 1;
        return true;
    }

    private void trickleUp(int index) {
        int parentIndex = (index - 1) / 2;
        Node bottom = heapArray[index];
        while (index > 0 && bottom.getData() > heapArray[parentIndex].getData()) {
            // move smaller parent downwards.
            heapArray[index] = heapArray[parentIndex];
            index = parentIndex;
            parentIndex = (parentIndex - 1) / 2;
        }
        heapArray[index] = bottom;
    }
/// Top 10 elements of heapArray is the result. You can modify the Node to hold Item as well as the value

- techpanja November 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 4 votes

Complexity: Find min/max: O(1), Delete: O(log N), INSERT: O(log N)
Space: O(N)

- techpanja November 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Time: O(N log N) to build the heap from given array
Space: O(N)

A much better method is to maintain a K sized min heap and update it as described by anon below, to get time: O(N log K) and space: K

- manungnv April 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simply build a Max Heap.
T.C
O(n) + O(log k)
=> O(n) overall.

- teli.vaibhav November 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Double check :)

- urik on bbery November 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Find the kth element using order statistics and then partition around that element. O(n) time O(1) space.

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

// Prints top k integers from a given array of integers ranging from [0..100]
// Uses histogram instead of maxHeap since the possible values are limited to [0..100]

public class Histogram {
	private static  int bucketSize =  101;
	private static int[] histo = new int[bucketSize];
	
	public static void buildHisto(int arr[], int k) {
	
		for (int i = 0; i < bucketSize; i++) { // initialize histo
			histo[i] = 0;
		}
		
		int i = 0;
		int arrLen = arr.length;
		while ((i < arrLen) && (histo[bucketSize-1] < k)) {
			(histo[arr[i]])++;
			i++;
		} //while
	} //buildHisto
	
	public static void printTop (int arr[], int k) {
		int arrLen = arr.length;
		k = min (k, arrLen);
		while (k > 0) {
			for (int j = bucketSize-1; j >= 0; j--) {
				for (int l = 0; l < histo[j]; l++) {
					System.out.println(j);
					k--;
					if (k <= 0) return;
				}
			}
		}
	} //printTop
	 
	private static int min(int int1, int int2) {
		if (int1 < int2)
			return int1;
		else return int2;
	}

	public static void main(String[] args) {
		int array[] = {0,1,2,3,4,5,6,7,8,9,10,12,11,10};
		int k = 5;
		buildHisto(array, k);
		printTop(array, k);
	}

}

Complexity: O(n), space: O(1)

- perf_guru November 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Nobody knows bucket sort here?....

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

You guys overcomplicate it
Here is simple solution in js

//Prepare array 
 var min = 0,
    max = 1000,
    i,
    arr = [];

for (i = min; i < max; i++)
{
    arr.push(i);
}
arr.sort(function(a, b) {
    return Math.random() > 0.5;
});

//Create simple kinda 'Heap'
var Heap = function(size){
    this.size = size;
    this.data = [];
};

Heap.prototype.push = function(item) {
    if (this.data.length < this.size) {
        this.data.push(item);
    } else {
        for (var i = 0; i <  this.size; i++) {
            if (this.data[i] < item) {
                this.data[i] = item;
                return;
            }
        }
    }
};

Heap.prototype.print = function() {
    console.log(this.data);
};
///Then iterate over array and push items in heap
var heap = new Heap(10);
arr.forEach(function(item) {
    heap.push(item);
});

heap.print();

- nikita.groshin July 24, 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