Hardware interview practice
Model a token bucket with one-time aging bypass
Implement a token-bucket checker with capacity 16, four new tokens every ten cycles, one token consumed per send, and one no-token bypass for a packet that has waited more than twenty cycles.
Reviewed example
Work through one case
Input
at cycle 21: tokens=0, packet id=9 has waited since cycle 0 after intervening legal sends consumed refills; send id9 twiceExpected output
first send uses its one aging bypass and removes id9; second send is rejected as unknown/duplicateAge 21 is greater than twenty and no token exists, so exactly one bypass is legal; successful removal prevents replay.
What to cover
Requirements
- Apply every elapsed refill interval, cap the bucket at 16, and preserve the ten-cycle refill phase.
- Track enqueue time by packet ID and reject sends for packets that were never enqueued.
- Permit a bypass only when no token exists, age is greater than 20, and that packet has never bypassed.
- Remove successful packets from the wait table and reject duplicate sends.
