Worst Fit Memory Allocation is a classic greedy strategy from operating systems. When several free memory blocks could hold a process, Worst Fit deliberately picks the largest one — the opposite of being frugal — so that the leftover gap stays big enough to be useful later.
Problem. You have a list of free memory blocks (their sizes) and a list of procs (process
sizes) arriving in order. For each process, place it in the largest free block that can hold it,
then shrink that block by the process size. Return, for each process, the index of the block it landed
in (or -1 if none fit).
Example: blocks = [100, 500, 200], procs = [212, 417, 112] → [1, -1, 1]. The 212 and 112 both
land in block 1 (the big one); the 417 fits nowhere.
The slow way first
You could try to be clever: sort blocks, build a heap, or rebalance after every placement. But for a single placement the rule is simple — among all blocks that fit, take the biggest. A plain scan over every block finds that biggest-fitting block directly, no extra structure needed.
The question to ask: given this one process, which open hole leaves me in the best shape afterward? Worst Fit answers "the biggest hole," reasoning that a big leftover is more reusable than a tiny sliver.
The idea: pick the biggest hole
Walk the processes in order. For each process of size, scan every block and remember the index of the largest block whose free size is at least size. If you found one, subtract size from it (that block now has a smaller leftover) and record the index. If no block was big enough, record -1.
The key insight: choosing the largest block (not the first or the tightest) keeps the remaining gaps spread out and large, which tends to leave room for future processes.
Walk through it
Step through the animation. The top row holds free blocks 100, 500, 200; the bottom row holds processes 212, 417, 112. Process 212 takes the largest block (500 → 288). Process 417 finds the largest remaining hole is only 288, which is too small, so it is rejected. Process 112 again takes block 1 (288 → 176).
Pseudocode
placement = empty list
for each process size in procs:
best = -1 # index of best block so far
for each block index j:
if blocks[j] >= size and (best is -1 or blocks[j] > blocks[best]):
best = j # a larger fitting block
if best is not -1:
blocks[best] = blocks[best] - size # shrink the chosen block
append best to placement
return placementThe Python solution
def worst_fit(blocks, procs):
placement = []
for size in procs:
best = -1
for j in range(len(blocks)):
if blocks[j] >= size and (best == -1 or blocks[j] > blocks[best]):
best = j
if best != -1:
blocks[best] -= size
placement.append(best)
return placementplacementcollects the chosen block index for each process (-1means rejected).- For every process we reset
best = -1, meaning "nothing chosen yet." - The inner loop scans all blocks. A block is a candidate only if
blocks[j] >= size(it can hold the process). - Among candidates we keep the one with the largest free size:
best == -1 or blocks[j] > blocks[best]. - If a block was chosen we shrink it with
blocks[best] -= size; otherwisebeststays-1.
Complexity
| Case | Time | Notes |
|---|---|---|
| Per process | O(b) (moderate) | scan all b blocks once |
| All processes | O(p * b) (moderate) | p processes, b blocks |
O(1) (fast)We use only a couple of variables beyond the inputs, so extra space is O(1). With a max-heap of block sizes you could speed the per-process pick to O(log b), but the plain scan is simplest and clear.
When this pattern shows up
Allocation and scheduling questions often boil down to a greedy choice: First Fit (first block that fits), Best Fit (smallest that fits), and Worst Fit (largest that fits) are the three classic moves. Recognize which one the prompt asks for, then it is just a scan that keeps the best candidate.
Worst Fit keeps gaps large, but it does not guarantee every process gets placed. If even the biggest
remaining hole is smaller than the process, that process must be rejected — record -1 and move on.
Practice
blocks = [100, 500, 200], after placing 212 the blocks are [100, 288, 200]. Where does process 417 go?
1. Which block does Worst Fit choose for a process?
2. Why pick the largest block instead of the tightest?
3. For blocks = [100, 500, 200] and procs = [212, 417, 112], what is the result?
4. What is the extra space used by this solution?