Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Fisher-Yates Shuffle proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Finalize one suffix position per uniform draw from the remaining prefix. Use inclusive bounds and a real swap, preserve the caller when the contract requires a copy, and derive equal permutation probability.
1 practice exercise1 interview problem
Recognize it when The task needs a random permutation of all input positions.
Checking your account…
Fisher-Yates processes positions from the end toward the front. For position i, it chooses uniformly from every index zero through i that has not yet been finalized.
Swap the chosen item into position i, then never touch that suffix position again. The remaining prefix contains exactly the items still available for earlier positions.
def shuffle_trace(values,draws):
values=values.copy();trace=[]
for offset,index in enumerate(range(len(values)-1,0,-1)):
chosen=draws[offset];values[index],values[chosen]=values[chosen],values[index];trace.append([index,chosen,values.copy()])
return traceImplement shuffle_trace(values,draws). Moving right to left, draws provides j in [0,i]; return [i,j,state] after each swap.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def shuffle_trace(values,draws):
values=values.copy();trace=[]
for offset,index in enumerate(range(len(values)-1,0,-1)):
chosen=draws[offset];values[index],values[chosen]=values[chosen],values[index];trace.append([index,chosen,values.copy()])
return tracePredict both states of the existing trace Lab. Each row copies the entire current array, so this teaching trace deliberately stores more than the ordinary linear shuffle.
| Step | Executed statement | State | Why |
|---|---|---|---|
| Protect caller | values=values.copy();trace=[] |
working list=["a","b","c"] |
The Lab reorders a separate top-level list. |
| Finalize index 2 | chosen=draws[offset];values[index],values[chosen]=values[chosen],values[index];trace.append([index,chosen,values.copy()]) |
i=2, j=1, state=["a","c","b"] |
b moves to its final position; future choices cannot touch index 2. |
| Finalize index 1 | chosen=draws[offset];values[index],values[chosen]=values[chosen],values[index];trace.append([index,chosen,values.copy()]) |
i=1, j=0, state=["c","a","b"] |
Only the two remaining prefix items exchange positions. |
| Complete | return trace |
two snapshots; empty-input trace=[] |
The remaining index 0 is forced, so no final random draw is needed. |
| Output | |
|---|---|
print(shuffle_trace(["a","b","c"],[1,0])) |
[[2, 1, ['a', 'c', 'b']], [1, 0, ['c', 'a', 'b']]] |
print(shuffle_trace([],[])) |
[] |
Uniformity concerns the set of remaining original positions. Finite deterministic draw lists test exact swaps; a chosen random source must separately meet the probability premise. Python random.shuffle mutates its argument, while this Lab returns copied ordering.
The draw must include both zero and i. Excluding i or choosing from the entire array each time makes permutation probabilities unequal.
def shuffled(values,draws):
output=values.copy()
for offset,index in enumerate(range(len(output)-1,0,-1)):
chosen=draws[offset];output[index],output[chosen]=output[chosen],output[index]
return outputImplement shuffled(values,draws). Return a shuffled copy using supplied inclusive draws. Supply one integer draw j in [0,i] for each descending index i from len(values)-1 through 1, in that order; empty or singleton input needs no draws.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def shuffled(values,draws):
output=values.copy()
for offset,index in enumerate(range(len(output)-1,0,-1)):
chosen=draws[offset];output[index],output[chosen]=output[chosen],output[index]
return outputClassic Fisher-Yates mutates in place with O(1) auxiliary space. Copy first when caller input must remain unchanged, and inject the random source or draws for deterministic tests.
Say: “At step i, each remaining item has equal probability of taking final position i. One uniform inclusive draw and swap finalizes it.” State O(n) time, mutation policy, RNG quality, and why repeated arbitrary swaps are biased.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Fisher-Yates Shuffle 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 |
|---|---|---|---|
| In-place shuffle | O(n) | O(n) | One bounded-cost draw and swap per suffix position. |
| Displayed swap trace | O(n^2) | O(n^2) | Each of n-1 iterations copies the full n-item state into the trace. |
O(1) in-place algorithm state; O(n) copying shuffle result; O(n^2) returned full-state trace.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
At i=2, every remaining item including the one already there needs probability 1/3. range(i) forbids leaving it in place.
Prevent it: Include i with range(i+1) or randrange(i+1).
choices=list(range(index))choices=list(range(index+1))Where you will hit this: Simulate Fisher-Yates Shuffling(opens in a new tab)
Assigning values[i] first destroys the old value needed for values[j], producing duplicate items.
Prevent it: Use simultaneous tuple assignment.
values[i]=values[j]
values[j]=values[i]values[i],values[j]=values[j],values[i]Where you will hit this: Simulate Fisher-Yates Shuffling(opens in a new tab)
output=values names the same list. A correct swap still rearranges the caller and violates the copy contract.
Prevent it: Copy the top-level list before swapping.
output=valuesoutput=values.copy()Where you will hit this: Shortest Subarray With OR at Least K(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27