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)Find one rank by partitioning and discarding the side that cannot contain it. Keep zero-based target indexes separate from one-based kth input and count duplicates as separate ranked entries.
1 practice exercise4 interview problems
Recognize it when Only the kth value is required, not a complete sorted list.
Checking your account…
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. values must be nonempty and 0 <= k < len(values).
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
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 tracePredict the actual trace Lab with target index 2. This original input produces a small pivot, then a large pivot, then the desired pivot; the target index never changes because the Lab uses absolute array indexes.
| Step | Executed statement | State | Why |
|---|---|---|---|
| Target rank | values=values.copy();left=0;right=len(values)-1;trace=[] |
target k=2, active [0,3] |
The trace interface already uses a zero-based absolute target. |
| Pivot 1 | values[boundary],values[right]=values[right],values[boundary] |
values=[1, 2, 5, 6], boundary=0 |
Only the right side can contain target 2. |
| Discard left | if boundary<k:left=boundary+1 |
active [1,3] |
The finalized pivot is excluded. |
| Pivot 6 | else:right=boundary-1 |
boundary=3, active becomes [1,2] |
Only the left side can contain target 2. |
| Pivot 5 | if boundary==k:break |
boundary=2; trace has three partitions |
The requested rank is now finalized; neither other side needs sorting. |
| Output | |
|---|---|
print(quickselect_trace([6, 2, 5, 1], 2)) |
[[0, 3, 0], [1, 3, 3], [1, 2, 2]] |
The kth-value Lab translates its one-based input once. A list-group implementation would instead translate k after discarding lower groups; do not mix that different representation with this absolute-index code.
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. values must be nonempty and 1 <= k <= len(values).
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
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-1For distinct keys, random pivots yield expected O(n) work because only one shrinking side is processed. The actual two-way at-most-pivot scheme can remain quadratic on all-equal input even with random pivot positions: pivot rank repeatedly lands at an endpoint. A three-way equal band can finish every target within that band immediately. The deterministic Lab also has quadratic adversarial cases; partition selection is not a stable ordering operation.
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 |
|---|---|---|---|
| Randomized quickselect | O(n) | O(n^2) | The expectation requires randomized pivots and distinct keys or duplicate-aware partitioning. The actual last-pivot <= Lab still scans near-full sides on all-equal inputs. |
O(n) for the copied input plus O(1) iterative partition state; trace rows add up to O(n).
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Rank 1 means the first value at zero-based index 0, not index 1.
Prevent it: Translate once at the interface: target = k - 1.
target = ktarget = k - 1Where you will hit this: Select the Kth Smallest Value(opens in a new tab)
[2,2,5] has second-smallest value 2. Removing duplicates changes the rank contract.
Prevent it: Retain multiplicities throughout partitioning.
answer = sorted(set(values))[k - 1]answer = sorted(values)[k - 1]Where you will hit this: Select the Kth Smallest Value(opens in a new tab)
If pivot index 0 is below target, left = pivot can repeat the same range.
Prevent it: Start the right side strictly after the pivot.
left = pivot_indexleft = pivot_index + 1Where 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