Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Solution: Kth Smallest Number
We'll cover the following
Problem Statement
Solution
Brute-force
2. Brute-force using Sorting
3. Using Max-Heap
4. Using Min-Heap
5. Using the Partition Scheme of Quicksort
6. Using Quicksort's Randomized Partitioning Scheme
7. Using the Median of Medians
Conclusion

Problem Statement

Given an unsorted array of numbers, find Kth smallest number in it.

Please note that it is the Kth smallest number in the sorted order, not the Kth distinct element.

Example 1:

Input: [1, 5, 12, 2, 11, 5], K = 3
Output: 5
Explanation: The 3rd smallest number is '5', as the first two smaller numbers are [1, 2].

Example 2:

Input: [1, 5, 12, 2, 11, 5], K = 4
Output: 5
Explanation: The 4th smallest number is '5', as the first three smaller numbers are
[1, 2, 5].

Example 3:

Input: [5, 12, 11, -1, 12], K = 3
Output: 11

.....

.....

.....

Like the course? Get enrolled and start learning!