vehicle_arrived.vehicle_left and closes the session so billing is computed from actual duration.lot_id so each lot is an independent unit of scale.Estimate the scale of the system. Consider daily active users, read/write ratio, storage requirements, bandwidth, and any relevant QPS calculations...
1. Create parking lot
POST /api/parking-lot{ "slot": 50 }200 OK: { "status": "50 slot created" }400 invalid/malformed URL, 429 rate limit exceeded2. Assigning parking lot
POST /api/assign-parking{ "licence-plate": "xxx" }200 OK: { "licence-plate": "xxx", "slot-number: 15}3. Get which parking lot
GET /api/parking-lot/{license-plate}200 OK: { "licence-plate": "xxx", "slot-number: 15}User searches → API Gateway authenticates and rate-limits → Reservation Service reads cached capacity → user reserves a spot (state: HELD) → Transaction Service initiates payment with the PSP → PSP webhook confirms → Reservation Service marks it CONFIRMED and decrements committed capacity. At the gate, the device calls Reservation Service to validate the reservation before lifting the barrier.
Choice: relational (PostgreSQL), single logical schema. Allocation needs ACID plus a uniqueness constraint to make double-booking impossible at the storage layer. Fast "find a vacant spot" reads are served by a Redis counter and read replicas on top — not by swapping to NoSQL, which would cost you the constraint that actually solves the hard problem.
| Entity | Key attributes |
lot | lot_id PK, name, capacity, contact_info_id FK |
spot | spot_id PK, lot_id FK, floor, spot_type, status (FREE/OCCUPIED) |
vehicle | vehicle_id PK, license_plate UNIQUE, type, user_id FK |
user | user_id PK, name, contact_info_id FK |
contact_info | contact_info_id PK, phone, email |
reservation | reservation_id PK, lot_id, spot_id FK, vehicle_id FK, user_id FK, start_time, end_time, payment_status, completion_status (HELD/CONFIRMED/COMPLETED/EXPIRED) |
transaction | transaction_id PK, reservation_id FK, vehicle_id, user_id, amount, check_in, check_out, status |
I'd deep-dive three: (1) the Spot Allocation Engine, (2) the Gate Controller, (3) the Availability/Capacity Service. The interviewer spends most of their attention on #1.
The mechanism (this is the whole point of the section): the database is the arbiter, not the application. In PostgreSQL:
CREATE EXTENSION btree_gist;ALTER TABLE reservation ADD CONSTRAINT no_overlap EXCLUDE USING gist ( spot_id WITH =, tstzrange(start_time, end_time, '[)') WITH && ) WHERE (completion_status IN ('HELD','CONFIRMED'));
The transaction does SELECT ... FOR UPDATE SKIP LOCKED over candidate spots, inserts the hold, and commits. If two users pick the same spot+window, the second insert violates no_overlap (SQLSTATE 23P01) → that request gets a 409 with the next available alternatives, the first keeps the hold. Winner and loser are decided at commit, by the DB.
Check-then-commit race eliminated: the "is a spot free?" availability test happens inside the same transaction as the insert, and the constraint is the final gate. Never: read availability → return to client → insert later.
Flow for the race:
Hot-lot surge on one window (say a concert ends and 200 users hit the same lot in 10 s): partition the work by lot_id and serialize per lot with a bounded queue; the tail sheds with fast 409 + Retry-After instead of piling onto the DB. Constraint-violation retries use jittered exponential backoff with a cap of ~3 attempts, then fall back to "any spot in this lot." Allocation stays correct because contention only changes who wins, never whether two people can win.
Cache is advisory: Redis holds a per-lot free-spot counter and a spot bitmap for search/UI. It may be stale by seconds — that only causes a retry, because admission is decided by the DB write above.
Idempotency: client sends Idempotency-Key; UNIQUE(idempotency_key) on reservation. A retried reserve returns the same hold ID rather than creating a second hold.
Hold TTL: holds last 10 minutes. A sweeper every 30 s flips HELD → EXPIRED, and every commit query additionally ignores holds where now() > expires_at — belt and suspenders, so a stalled sweeper can't leak inventory. Renewal re-validates under the same exclusion constraint.
Interval semantics: half-open [start, end). 11:00–12:00 and 12:00–13:00 coexist; 11:00–12:01 overlaps. Overlap = start_a < end_b AND start_b < end_a.
Scale sanity: 500 spots/lot, 20 req/s peak per lot on a hot window, ~50 ms per commit → a single writer per lot is nowhere near saturation, which is why I don't need a distributed lock service.
Tradeoff table worth saying out loud:
| Approach | Pro | Con |
| Exclusion constraint (chosen) | Correct by construction, no app locking | Needs range/GiST support; conflict surfaces as an error |
SELECT FOR UPDATE row lock | Simple, familiar | Lock on a spot row blocks reservation inserts for adjacent windows too; deadlock risk |
| Per-lot single writer / actor | Trivial reasoning, cheap | Hot-lot throughput bounded; extra infra |
| Optimistic version column | No locks | Requires a free-slot index ritual; thundering retries |
Edge device at the barrier: camera/ALPR or QR scan → POST /api/gate/validate → opens in p95 < 300 ms, or the driver sits there honking. So the controller keeps a local signed allowlist cache of confirmed reservations for its lot (refreshed every 10 s + on push). If the cloud is unreachable, it admits against the cache and reconciles on reconnect — a 20-minute-old signed token can't be forged, and any over-admission is captured as an exception record rather than a locked barrier. Exit is always permitted (never trap a car over a payment glitch); unpaid sessions become a debt record.
Per-lot free-spot counter in Redis (INCR/DECR, not read-modify-write) plus a spot_id → status bitmap for "nearest free spot" queries, refreshed from DB changes. It serves search, dashboards, and the "lot is full" sign. It is explicitly not on the admission path — that's the whole reason it's allowed to be eventually consistent.