All spot and vehicle identifiers are nonempty strings. Kinds are exactly motorcycle, car, and van, ranked in that order. Invalid identifiers or kinds raise std::invalid_argument before mutation.

Prototype: implement choose_spot(spots, vehicle_kind). spots is a list of (spot_id, kind) pairs with unique identifiers; every listed spot is free. Return the selected spot identifier or nullopt. Use smallest compatible kind, then lexicographic identifier. Do not change the input. Validate every spot, including spots that cannot be selected; duplicate spot identifiers raise std::invalid_argument.

Final: implement ParkingLot(spots) with the same inventory validation; an empty inventory is valid. park(vehicle_id, kind) returns a fresh ticket value record with keys ticket_id, vehicle_id, vehicle_kind, and spot_id, or nullopt if no compatible spot is free. Ticket numbers start at 1 and advance only on success. A vehicle with an active ticket raises std::invalid_argument; after release it may park again. release(ticket_id) accepts an integer identifier and returns the released ticket as a fresh value record, or nullopt for an unknown or already released identifier. active_tickets() returns fresh value records in a fresh list ordered by ticket number. available_spots() returns a fresh list of (spot_id, kind) tuples ordered by kind rank, then identifier. Separate lots own separate state. Invalid requests and capacity failures leave state and the next ticket number unchanged.

The public C++ API below is authoritative for field types and return values. erase is used where another language might use a method named delete, because delete is a C++ keyword. Returned records are value snapshots with the documented named fields; mutating a record or its nested vectors cannot affect the service.

cpp
struct Ticket {Int ticket_id;std::string vehicle_id,vehicle_kind,spot_id;bool operator==(const Ticket&)const=default;};
inline Opt<std::string> choose_spot(const Vec<std::pair<std::string,std::string>>& spots,const std::string& kind);
class ParkingLot { public:
 explicit ParkingLot(Vec<std::pair<std::string,std::string>> spots);
 Opt<Ticket> park(const std::string& id,const std::string& kind);
 Opt<Ticket> release(Int id);
 Vec<Ticket> active_tickets() const;
 Vec<std::pair<std::string,std::string>> available_spots() const;
};

Compile and run from this directory:

Shell
c++ -std=c++20 -O2 -Wall -Wextra -pedantic -pthread main.cpp -o practice
./practice --stage prototype
./practice --stage final

Edit exercise.hpp. Both stages start unfinished and must report failures until implemented. Keep main.cpp as the executable contract. The runner returns nonzero for a failed check, an unexpected exception, an invalid stage, or an empty suite. C++20 and only the standard library are required.

Drag or use the arrow keys to resize. Home collapses the question; End collapses the editor. Enter or double-click resets the split.
Loading editor…
No results yet.

Complete the starter code, then run the checks.
Results and errors will appear here.

C++ runtime powered by Wasmer.