Home/Low-level design/Parking Lot

Parking Lot

Compatible allocation and ticket lifecycle

Separate immutable inventory from active allocation. A vehicle's eligibility compares size ranks; selection compares (rank, identifier), not input order. Ticket IDs identify allocation lifecycles and advance only after a successful commitment.

cpp
struct Candidate {int rank;std::string id;};
// Validate every inventory row before selecting any candidate.

Validate before checking capacity so malformed records do not hide behind a full lot. Prepare the selected spot before recording the active ticket. Removing a ticket releases exactly its recorded spot, rather than recomputing allocation. Return ticket records by value to prevent callers changing internal vehicle identity. Sequential atomicity here means failure leaves state unchanged; concurrent check-and-commit would additionally need one lock spanning both steps.

C++ contract conventions: numeric values use signed Int (long long); all inputs and intermediate arithmetic are within its representable range. Opt<T> means std::optional<T> and absence is std::nullopt; Vec<T> means std::vector<T>. Invalid documented arguments throw std::invalid_argument. Inputs passed by const reference remain caller owned. Returned containers and records own their contents. Calls use the declared C++ parameter types. Implicit conversions performed by the caller are outside the runtime validation contract. Unless a lesson states otherwise, string ordering is lexicographic over Unicode code points (Text) or UTF-8 identifiers (std::string).

Put it into practice

Implement the exercise in a full-screen workspace, then run its tests.

Open practice
Next lessonVending Machine