Skip to the guide

Allocating scarce things: spots, seats, rooms and slots

IntermediatePattern7 pages

Many LLD questions hand out scarce things, such as parking spots, lockers and appointment slots. When any free one of a kind will do, this move keeps the free ones in a list for each kind, so finding one is a lookup. It combines Strategy for the fit rule with a small in-memory repository, a class that stores things and finds them, for the free lists. Interviewers listen for who gets the last one, and for when a plain loop is enough.

The shoe counter at a bowling alley

Think of the counter at a bowling alley, where you rent shoes for the evening. Behind the clerk is a rack with one shelf for each size. You say "42", and the clerk reaches straight to that shelf without looking at any other pair. If it is empty, the house rule says to try 43, which is a little loose but works, and never 41. The clerk writes your name beside the pair's number, and the shoes go back on their shelf when you return them. So a pair is always on the rack or on someone's feet.

In code, the counter is this page's Marina, and each pair of shoes is a Berth. The rack is FreeBerths, with one stack of free berths for each size. Your shoe size is the boat's Size, and the house rule is a FitRule. The clerk's list of names is held, a map from each boat to its berth.