Implement Stock in BOTH stages: nonnegative capacity, used initially zero, reserve(amount>=0) returns false unchanged when insufficient, release(amount) requires 0<=amount<=used, snapshot returns values. Prototype constructs a stock and attempts reservations in order, ignoring capacity failures. Final solve borrows an existing Stock, reserves amount, and if accepted uses a scope guard to release it on every exit. If fail_inside is true after a successful reservation, throw runtime_error; a capacity failure does not throw. On successful normal return, return the snapshot observed inside the reservation, then release before the caller resumes. An invalid amount throws invalid_argument unchanged. Stock must outlive the scope guard.

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. No runtime type coercion is required; C++ static types exclude many dynamic-language type errors. Unless a lesson states otherwise, string ordering is lexicographic over Unicode code points (Text) or UTF-8 identifiers (std::string).

cpp
struct ReservationSnapshot {Int capacity,used;bool operator==(const ReservationSnapshot&)const=default;};
class Stock { public:
explicit Stock(Int capacity);
bool reserve(Int amount);
void release(Int amount);
ReservationSnapshot snapshot() const;
};
inline ReservationSnapshot prototype(Int capacity,const Vec<Int>& reservations);
inline ReservationSnapshot solve(Stock& stock,Int amount,bool fail_inside);

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.