Tracing State Through Rejected Calls

A ticketing system models one block of seats with the class below. Its invariant is that held and reserved are never negative and held + reserved <= capacity.


Python
class SeatBlock:
    def __init__(self, capacity, reserved):
        self.capacity = capacity
        self.reserved = reserved
        self.held = 0

    def hold(self, n):
        if n <= 0 or self.held + self.reserved + n > self.capacity:
            raise ValueError
        self.held += n

    def confirm(self, n):
        if n <= 0 or n > self.held:
            raise ValueError
        self.held -= n
        self.reserved += n

    def release(self, n):
        if n <= 0 or n > self.held:
            raise ValueError
        self.held -= n


The object is created as SeatBlock(120, 70). The calls below are then made in order; each is wrapped so that a raised ValueError is caught and the program continues with the next call.


Call

Operation

1

hold(30)

2

hold(25)

3

confirm(20)

4

hold(20)

5

release(35)

6

confirm(25)

7

confirm(0)

8

release(2)


After call 8, give the final value of held, the final value of reserved, the number of free seats (capacity minus held minus reserved), and the number of calls that were rejected. Answer each as a whole number.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Class Invariants Questions: