Groupon Interview Question
Software Engineer / DevelopersCountry: United States
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).
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
// 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)
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();
Since the values are in a tight range you probably want to do either of :
- S O U N D W A V E November 05, 20131) 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).