What an LLD interview tests
easy 40 minA low-level design round gives you one vague sentence and about 45 minutes.
Watch lesson5:00
Bike share, complete
Read it, run it, change a line and run it again.
Loading editor…
Run it: one bike, two riders, a full station.
The same in Java
import java.util.*;enum BikeState { DOCKED, RIDING, REPAIR }class Refused extends RuntimeException { Refused(String why) { super(why); }}class Bike { private static final Map<BikeState, Set<BikeState>> NEXT = Map.of( BikeState.DOCKED, Set.of(BikeState.RIDING, BikeState.REPAIR), BikeState.RIDING, Set.of(BikeState.DOCKED), BikeState.REPAIR, Set.of(BikeState.DOCKED)); final String id; private BikeState state = BikeState.DOCKED; Bike(String id) { this.id = id; } BikeState state() { return state; } void moveTo(BikeState next) { if (!NEXT.get(state).contains(next)) { throw new Refused(id + ": " + state + " to " + next); } state = next; }}class Station { final String id; private final int docks; private final Map<String, Bike> bikes = new LinkedHashMap<>(); Station(String id, int docks) { this.id = id; this.docks = docks; } void put(Bike bike) { if (bikes.size() >= docks) throw new Refused(id + " is full"); bikes.put(bike.id, bike); } Bike take() { for (Bike bike : bikes.values()) { if (bike.state() == BikeState.DOCKED) { bikes.remove(bike.id); return bike; } } throw new Refused(id + " has no bike ready"); }}interface PricingRule { int feeCents(int minutes);}class PerMinute implements PricingRule { private final int cents, free; PerMinute(int cents, int freeMinutes) { this.cents = cents; this.free = freeMinutes; } public int feeCents(int minutes) { return Math.max(0, minutes - free) * cents; }}record Ride(String rider, Bike bike, int started) {}class RideService { private final Map<String, Station> stations = new HashMap<>(); private final Map<String, Ride> rides = new HashMap<>(); private final PricingRule pricing; RideService(List<Station> all, PricingRule pricing) { for (Station s : all) stations.put(s.id, s); this.pricing = pricing; } synchronized Ride start(String rider, String stationId, int now) { if (rides.containsKey(rider)) throw new Refused(rider + " already rides"); Bike bike = station(stationId).take(); bike.moveTo(BikeState.RIDING); Ride ride = new Ride(rider, bike, now); rides.put(rider, ride); return ride; } synchronized int finish(String rider, String stationId, int now) { Ride ride = rides.get(rider); if (ride == null) throw new Refused(rider + " has no ride"); int fee = pricing.feeCents(now - ride.started()); station(stationId).put(ride.bike()); ride.bike().moveTo(BikeState.DOCKED); rides.remove(rider); return fee; } private Station station(String id) { Station s = stations.get(id); if (s == null) throw new Refused("no station " + id); return s; }}The same in TypeScript
type BikeState = "docked" | "riding" | "repair";class Refused extends Error {}const NEXT: Record<BikeState, BikeState[]> = { docked: ["riding", "repair"], riding: ["docked"], repair: ["docked"],};class Bike { private current: BikeState = "docked"; constructor(readonly id: string) {} get state(): BikeState { return this.current; } moveTo(next: BikeState): void { if (!NEXT[this.current].includes(next)) { throw new Refused(`${this.id}: ${this.current} to ${next}`); } this.current = next; }}class Station { private bikes = new Map<string, Bike>(); constructor(readonly id: string, private readonly docks: number) {} put(bike: Bike): void { if (this.bikes.size >= this.docks) throw new Refused(`${this.id} is full`); this.bikes.set(bike.id, bike); } take(): Bike { for (const bike of this.bikes.values()) { if (bike.state === "docked") { this.bikes.delete(bike.id); return bike; } } throw new Refused(`${this.id} has no bike ready`); }}interface PricingRule { feeCents(minutes: number): number;}class PerMinute implements PricingRule { constructor(private readonly cents: number, private readonly free = 0) {} feeCents(minutes: number): number { return Math.max(0, minutes - this.free) * this.cents; }}interface Ride { readonly rider: string; readonly bike: Bike; readonly started: number }// No lock: each method runs to its end before another starts, as there is no await inside.class RideService { private stations = new Map<string, Station>(); private rides = new Map<string, Ride>(); constructor(all: Station[], private readonly pricing: PricingRule) { for (const s of all) this.stations.set(s.id, s); } start(rider: string, stationId: string, now: number): Ride { if (this.rides.has(rider)) throw new Refused(`${rider} already rides`); const bike = this.station(stationId).take(); bike.moveTo("riding"); const ride = { rider, bike, started: now }; this.rides.set(rider, ride); return ride; } finish(rider: string, stationId: string, now: number): number { const ride = this.rides.get(rider); if (!ride) throw new Refused(`${rider} has no ride`); const fee = this.pricing.feeCents(now - ride.started); this.station(stationId).put(ride.bike); ride.bike.moveTo("docked"); this.rides.delete(rider); return fee; } private station(id: string): Station { const s = this.stations.get(id); if (!s) throw new Refused(`no station ${id}`); return s; }}