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.
Starting from an empty set, each implementation receives insert(40), insert(15), insert(72), insert(8), insert(15) and insert(33), then contains(33), contains(50) and contains(8).
Counting only the elements examined during the three contains calls, give the total number of elements examined by implementation A and by implementation B, each as a whole number. Also give the result returned by contains(50), as true or false.
Unlock full course notes by signing up
Trace a credit-pool contract
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.
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.
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.