A laboratory's CreditPool ADT starts with 20 available credits. For positive integer k, spend(k) returns true and subtracts k if enough credits are available; otherwise it returns false and changes nothing. refund(k) adds k, returns no value, and requires k not to exceed the number already spent.
The calls in the table run consecutively on the same pool. Complete each missing result using true or false, and each availability using an exact integer number of credits.
Unlock full course notes by signing up
Recover an unknown capacity
A PermitPool ADT begins with all C permits available, where C is an unknown positive integer. For positive integer k, take(k) returns true and removes k permits when enough are available; otherwise it returns false and leaves availability unchanged. available() reports availability without changing it.
Consecutive calls produce these results: take(7) returns true, take(8) returns false, take(4) returns true, and available() returns 3. What is the initial capacity C? Answer in permits, to zero decimal places.
Specify before you represent
A team is building an abstract data type that other programmers will use. They want client code to depend only on the type's behaviour, so that the internal representation can later be replaced without breaking any client. Each stage below uses the result of the stage before it. Arrange the stages in the order the team should carry them out.
Same results, different costs
An IntSet abstract data type has two operations: insert(x) adds the integer x if it is not already a member and otherwise changes nothing, and contains(x) returns true if x is a member and false otherwise. Two implementations meet this specification.
Implementation A stores the members in an unsorted array. insert(x) appends x to the end if it is not already present. contains(x) examines elements from index 0 onward and stops when it finds x or reaches the end of the array.
Implementation B stores the members in an array sorted in ascending order. insert(x) places a new member in its sorted position. contains(x) examines elements from index 0 onward and stops at the first element greater than or equal to x, returning true only if that element equals x.
Check interchangeable implementations
A PermitPool ADT has capacity C and initially has C free permits. For every positive integer k, take(k) returns true and removes k permits if at least k are free; otherwise it returns false without changing availability. available() returns the number free without changing the state. These are its only public operations.
Which implementations satisfy this contract for every permitted sequence of calls? Select all that apply. Judge only the specified results and state changes; ignore running time and storage use.