DSA interview question
A delivery app wants the k-th fastest delivery time of the day to set a realistic promise for customers. Sorting all times is O(n log n). Keeping a max-heap of the k smallest times seen so far is O(n log k): its root is the largest of those k, which is exactly the k-th smallest. Any new time smaller than the root replaces it. It's the mirror image of the k-th largest problem.
Example: [7, 10, 4, 3, 20, 15], k = 3 → 7.
int kthSmallest(const vector<int>& arr, int k); // 1 <= k <= n
Input (arr, k) | Output | Explanation |
|---|---|---|
([7, 10, 4, 3, 20, 15], 3) | 7 | Sorted: 3, 4, 7, … |
([7, 10, 4, 20, 15], 4) | 15 | |
([1], 1) | 1 | |
([5, 5, 5, 1], 2) | 5 | Duplicates count |
([2, -1, 0], 1) | -1 | The minimum |
(Confirm: k-th smallest counting duplicates; 1 ≤ k ≤ n.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in