Curriculum

Curriculum

Record the State Before and After an Action

Scenario

Study Room B starts with freeSeats = 1. Two students submit booking requests in sequence.

  1. 1Initial state before requests
  2. 2Request 1 arrives (valid)
  3. 3Request 2 arrives (room now full)

Try it yourself

Step through both requests and predict freeSeats after each one.

Reveal the explanation
  1. Initial state before requests

    Room B has freeSeats = 1.

  2. Request 1 arrives (valid)

    Decision: Accept. Stored state updates to freeSeats = 0.

  3. Request 2 arrives (room now full)

    Decision: Decline. Stored state remains freeSeats = 0 (never becomes -1).

Always check that a declined request leaves state untouched. Subtracting a seat on a declined request creates impossible negative state.

Your turn

Trace a library reader starting at borrowed = 2 through two consecutive checkout requests for available books.

Common mistake Incrementing borrowed count even when the second request is declined at the limit.

Check your work

After request 1: Accept, borrowed = 3. After request 2: Decline, borrowed stays 3.

Quick check

A study room has freeSeats = 0. A booking request arrives and is declined. What must freeSeats be afterward?
Show the answer and reasoning

Correct answer 0 (unchanged)

Declined requests must not mutate the seat count.