Common Coding Interview Questions Kth Element
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Top elements and k -th element
Two twists on the same idea that show up frequently in coding interviews in various incarnations:
- given an unsorted array of objects of length
, find the topn objects based on a given comparison functionk - given an unsorted array of objects of length
, findn -th object based on a given comparion function, that is an object that would appear at thek -th place in an sorted arrayk
You can expect
In this article, we will go through 3 solutions to these problems:
- a trivial solution with
complexityO ( n ∗ l o g ( n ) ) - a medium complexity solution with
complexityO ( n ∗ l o g ( k ) ) - a complex solution that relies on prior knowledge of specific algorithms with
complexityO ( n )
All solutions are presented using an array of ints to minimize amount of boilerplate.
Trivial solution O ( n ∗ l o g ( n ) )
The trivial solution is given to us on a silver plate in the assignment text.
If the array would be sorted, returning either the
So let's just do that. Sort the array and simply return the
Complexity analysis of trivial solution
We sort the array, which has
Medium solution O ( n ∗ l o g ( k ) )
When we consider optimizations, we need to look at the unnecessary work we are doing. Since we do not care about the elements besides the top partial_sort
.
Complexity analysis of partial sort solution
Time complexity of partial_sort
is
Medium solution with O ( k ) space complexity
One problem with the previous straightforward solution is that we still need to make copy of the entire array, since partial_sort
is still destructive, leaving us with
We must store the top
Datastructure we require must have the ability to insert elements, look a the smallest element and remove the smallest element all in priority_queue
(which is not necessarily implemented as a heap, but providing the same performance characteristics).
Complexity analysis of medium solution
We scan through all the elements of the array and at worst case we will insert each of them into the priority_queue
. This leads to total push()
& pop()
, each with priority_queue
.
Complex solution O ( n )
First observation is that if we know the
What would happen if we would just blindly guess that a particular element is the
What happens if we weren't correct? Well, we know on which side of the pivot our
This might sound very similar to the Quicksort algorithm and indeed, this is referred to as the Quickselect algorithm.
There is one more important step however. You might recall that Quicksort with a random pivot is
To fix this we need the median of medians algorithm.
Median of medians
The source for the worst case of Quickselect is a corner case when we keep choosing either the smallest or the largest element as our pivot. In that case we only improve the situation by one element, recursing over
What if we could pick the pivot somewhat well. Let's say that we always pick a pivot that splits that array in such a way that the larger portion has
The core idea of the median of medians is to process the array in chunks of 5 elements and pick medians from these 5 element sub-arrays. Due to the constant size, picking each of these medians is
Complexity analysis of complex solution
As already mentioned, due to the smart choice of our median, the time complexity is
Next:
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Šimon Tóth 2020 (kontakt@simontoth.cz)