Reported July 2025
ByteDancedivide and conquer

QuickSelect Kth Smallest

Reported by candidates from ByteDance's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ByteDance OA. Under 2s to a working solution.
Founder's read

The detail that matters in this ByteDance OA, reported July 2025, is the line about expected O(n) time. Sorting gives you the right answer and still fails the spec. The problem is Kth Smallest via QuickSelect: one-indexed k, duplicates counted as separate ranks, up to 100000 elements. If you know partition-based selection cold, it's ten minutes of work. If you blank on the partition loop, it gets ugly fast. StealthCoder sits invisibly on your screen during the live assessment as a safety net, so a mental freeze doesn't sink an otherwise easy problem.

The problem

Given an integer array nums and an integer k, return the kth smallest value in the array.
k is one-indexed. Duplicate values occupy separate ranks, just as they do in the fully sorted array.
Solve the problem with a selection algorithm whose expected running time is O(n).

Function
kthSmallest(nums: int[], k: int) → int

Examples
Example 1
nums = [3,2,1,5,6,4]
k = 2
return = 2
In sorted order the values are [1,2,3,4,5,6], so the second smallest value is 2.
Example 2
nums = [7,7,2,9,1]
k = 4
return = 7
The sorted ranks are [1,2,7,7,9]. Counting duplicates separately, rank 4 contains 7.

Constraints
1 <= nums.length <= 100000
-10^9 <= nums[i] <= 10^9
1 <= k <= nums.length

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is QuickSelect. Pick a pivot, partition the array around it, and look at where the pivot lands. Target index is k-1 since k is one-indexed. If the pivot lands there, return it. If it lands left, recurse right. Otherwise recurse left. You only chase one side, so the expected cost is O(n). The pitfall is duplicates. Example 2 has two 7s, and a naive two-way partition on arrays full of equal values degrades to O(n^2). Use a random pivot, and consider three-way partitioning so equal values collapse in one pass. Also watch the off-by-one on k. The hinted binary-search flavor is really binary search on rank position, not on a sorted array. If you freeze on the partition code mid-assessment, StealthCoder is the hedge that gives you a working version on screen.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill QuickSelect Kth Smallest cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as kth largest element in an array. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass ByteDance's OA.

ByteDance reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

QuickSelect Kth Smallest FAQ

What's the trick in the ByteDance QuickSelect problem?+

Partition around a pivot and only recurse into the side containing index k-1. That's what gives expected O(n) instead of O(n log n). Randomize the pivot so adversarial inputs don't hurt you, and handle duplicates cleanly.

Can I just sort the array and return nums[k-1]?+

It returns correct answers, but the statement explicitly asks for a selection algorithm with expected O(n) time. Sorting is O(n log n). If the assessment grades on spec or runs large tests, relying on it is a risk. Use it only as a fallback.

How do duplicates affect the answer?+

Duplicates take separate ranks. In [7,7,2,9,1] with k=4, the sorted order is [1,2,7,7,9], so the answer is 7. You don't dedupe. The danger is performance: many equal values can wreck a two-way partition, so use three-way partitioning.

Is a heap an acceptable alternative?+

A heap of size k runs in O(n log k), which beats sorting but still isn't expected O(n). It's a decent backup if QuickSelect goes sideways. For the stated requirement, though, QuickSelect is the intended answer.

How do I prepare for this in 48 hours?+

Write QuickSelect from memory three times: Lomuto or Hoare partition, random pivot, then the three-way variant. Test on [3,2,1,5,6,4], [7,7,2,9,1], and an all-equal array. Check k-1 indexing each time. That covers nearly every failure mode.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ByteDance.

OA at ByteDance?
Invisible during screen share
Get it