DescriptionQ878
Q878DVASIC interview problem
Model credit-based flow control
TechniquesPythonCreditsFlow controlInvariant
DifficultyMedium
TopicReference Models
LanguagePython
Requirements4 checkpoints
01
Problem
Implement a small reference model for a transmitter that may send only when at least one credit is available. Sending consumes one credit and asynchronous returns replenish credits.
Example input and output
Use this case to check your interpretationInput
capacity=2, initial=2; send(), send(), send(), return_credits(1)Output
credits: 1,0; third send raises underflow and stays 0; return restores credits to 1Explanation
The rejected send never makes the count negative, and every successful transition preserves 0 <= credits <= 2.
02
Requirements (4)
- Reject a send at zero credits without allowing the count to become negative.
- Reject negative credit returns and define whether returns may exceed the configured capacity.
- Expose the current count for scoreboard diagnostics.
- Maintain the invariant 0 <= credits <= capacity after every successful operation.
