Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Quickselect proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Partition like quicksort while recursing into only the side containing the requested rank. Learn the algorithm's preconditions, state transition, correctness argument, Python cost model, and transfer from one focused drill to a full Interview Problem.
Recognize it when
Consider Quickselect when the prompt's constraints and required operations match this shape: Partition like quicksort while recursing into only the side containing the requested rank.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Quickselect uses the same partition operation as quicksort, but only the side containing the target rank remains relevant.
After partition, the pivot has its final rank. If that rank is below target, discard it and the left side; if above, discard it and the right side.
def quickselect_trace(values,k):
values=values.copy();left=0;right=len(values)-1;trace=[]
while left<=right:
pivot=values[right];boundary=left
for index in range(left,right):
if values[index]<=pivot:values[boundary],values[index]=values[index],values[boundary];boundary+=1
values[boundary],values[right]=values[right],values[boundary];trace.append([left,right,boundary])
if boundary==k:break
if boundary<k:left=boundary+1
else:right=boundary-1
return traceImplement quickselect_trace(values,k). k is zero-based smallest rank; return [left,right,pivot_index] after each partition until found.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Translate one-based kth-smallest input to zero-based target exactly once. Kth largest can map to n minus k or invert the partition relation; mixing conventions causes off-by-one errors.
def kth_smallest(values,k):
values=values.copy();target=k-1;left=0;right=len(values)-1
while True:
pivot=values[right];boundary=left
for index in range(left,right):
if values[index]<=pivot:values[boundary],values[index]=values[index],values[boundary];boundary+=1
values[boundary],values[right]=values[right],values[boundary]
if boundary==target:return values[boundary]
if boundary<target:left=boundary+1
else:right=boundary-1Implement kth_smallest(values,k). k is one-based; return the kth smallest value without mutating input.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Random pivots yield expected O(n) work because only one shrinking side is processed. Adversarial pivots can cause O(n squared); randomization or median selection reduces risk but does not make the simple version stable.
Say: “Partition fixes one pivot rank. Only one side can contain target rank k, so I discard the other instead of sorting it.” State input mutation, duplicates, expected and worst-case time, and rank convention.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Quickselect proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Quickselect complete workflow | O(n) average, O(n^2) worst | O(n) average, O(n^2) worst | Three-way partition candidates and retain only the partition containing the one-based target rank. |
O(n) for the focused Select the Kth Smallest Value implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Verify algorithm preconditions such as sorted input, nonnegative weights, acyclicity, or admissible heuristics before applying it.
Prevent it: State and verify this precondition before coding: The comparison, key domain, stability need, and memory constraints match the selected ordering method.
Avoid
def kth_smallest(values, k):
passUse instead
def kth_smallest(values, k):
candidates = list(values)
while True:
pivot = candidates[len(candidates) // 2]
lower = [value for value in candidates if value < pivot]
equal = [value for value in candidates if value == pivot]
higher = [value for value in candidates if value > pivot]
if k <= len(lower):
candidates = lower
elif k <= len(lower) + len(equal):
return pivot
else:
k -= len(lower) + len(equal)
candidates = higherWhere you will hit this: Select the Kth Smallest Value(opens in a new tab)
Moves into the higher partition without translating k past discarded values.
Prevent it: Preserve this proof obligation: The resolved prefix, suffix, rank, digit pass, or bucket remains in its documented final relation.
Avoid
def kth_smallest(values,k):
a=list(values)
while True:
p=a[0]; lo=[x for x in a if x<p]; eq=[x for x in a if x==p]; hi=[x for x in a if x>p]
if k<=len(lo):a=lo
elif k<=len(lo)+len(eq):return p
else:a=hiUse instead
def kth_smallest(values, k):
candidates = list(values)
while True:
pivot = candidates[len(candidates) // 2]
lower = [value for value in candidates if value < pivot]
equal = [value for value in candidates if value == pivot]
higher = [value for value in candidates if value > pivot]
if k <= len(lower):
candidates = lower
elif k <= len(lower) + len(equal):
return pivot
else:
k -= len(lower) + len(equal)
candidates = higherWhere you will hit this: Select the Kth Smallest Value(opens in a new tab)
Include visited state, boundary cases, recursion depth, and hidden copying costs in correctness and complexity analysis.
Prevent it: Count every sort, slice, copy, membership check, heap update, and recursive frame before claiming O(n) average, O(n^2) worst.
Avoid
def kth_smallest(values,k):
a=list(values)
while True:
p=a[0]; lo=[x for x in a if x<p]; eq=[x for x in a if x==p]; hi=[x for x in a if x>p]
if k<=len(lo):a=lo
elif k<=len(lo)+len(eq):return p
else:a=hiUse instead
def kth_smallest(values, k):
candidates = list(values)
while True:
pivot = candidates[len(candidates) // 2]
lower = [value for value in candidates if value < pivot]
equal = [value for value in candidates if value == pivot]
higher = [value for value in candidates if value > pivot]
if k <= len(lower):
candidates = lower
elif k <= len(lower) + len(equal):
return pivot
else:
k -= len(lower) + len(equal)
candidates = higherWhere you will hit this: Build Tree from Inorder and Postorder(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27