Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Sort ascending and return `nums[k-1]`. O(n log n). For large data, quickselect is O(n) average and a min-heap is O(n + k log n).
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you handle the 1/0 indexing and know faster order-statistic methods.
Where candidates slip
The k vs k-1 off-by-one, or sorting in place destructively.
How to stand out
Describe quickselect's average O(n) and the size-k heap.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Sort and index
1Hint 1Explore hintHide hint
Where does the k-th smallest sit in a sorted array?
2Hint 2Explore hintHide hint
Sort and index k−1 is the baseline — what's the 1-vs-0 indexing?
3Hint 3Explore hintHide hint
Do you need to sort everything for one order statistic?
A · Core idea
The k-th smallest element sits at index k−1 once the array is sorted, so sort and index is the clean O(n log n) baseline. Faster methods exist for a single order statistic.
Why sort-and-index is correct. Sorting ascending arranges elements by rank; the element at 0-based index k−1 is the k-th smallest (1-indexed k). Duplicates count toward the rank, which is the usual convention.
Why mention quickselect / a heap. If you only need one order statistic, sorting everything is overkill. Quickselect partitions like quicksort but recurses into only one side, averaging O(n). A bounded max-heap of size k gives O(n log k). Naming these shows you know sorting isn't always necessary.
Don't mutate the caller's array unless allowed — sort a copy.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [3, 1, 2], k = 2sort
[1, 2, 3]
sorted2C · Code with teaching comments
D · Why it works and complexityO(n log n) timeO(1) – O(n) spaceOpen the invariant and proofExplore the proofHide the proof
Naming quickselect / a bounded heap shows you know sorting isn't always necessary for a single order statistic.
ComplexityO(n log n) time, O(1) – O(n) space — sorting is O(n log n); quickselect averages O(n); a size-k heap is O(n log k).
E · Edge cases and pitfalls4nums = [5], k = 1 — Only element → 5.Explore edge casesHide edge cases
Edge cases handled
nums = [5], k = 1Only element → 5.nums = [4, 4, 4], k = 2Duplicates count → 4.
Pitfalls
- Off-by-one: returning nums[k] instead of nums[k-1].
- Mutating the caller's array when a copy is expected.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int kthSmallest(int[] nums, int k) { int[] copy = nums.clone(); java.util.Arrays.sort(copy); return copy[k - 1]; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Off-by-one: returning nums[k] instead of nums[k-1].
- Mutating the caller's array when a copy is expected.
Revise
Carry the pattern into the interview
Pattern to remember
Sort then index (order statistic)
Code pattern to memorize
sorted(nums)[k-1] (or quickselect for O(n) avg)Rules to remember
- 1Sort ascending.
- 2k-th smallest = index k-1.
- 3Quickselect for O(n).
Use this pattern when
- Order statistics
Choose another pattern when
- Streaming / huge data — use a bounded heap or quickselect
Interview takeaway
k-th smallest = sort and index; quickselect if you need speed.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Sort then index (order statistic). Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Kth Largest ElementComing soonHeap or quickselect; symmetric to smallest.