Code RoomBounded buffer simulation
HardPrep Room Coding #1019

Bounded buffer simulation

CodingConcurrencySenior–Staff~40 min

Simulate a deterministic bounded-buffer producer/consumer over a sequence of operations. You are given `ops`, a list of strings each either 'P' (produce) or 'C' (consume), and an integer `cap` (buffer capacity). Process ops left to right maintaining a current item count: a 'P' succeeds (count += 1) only if count < cap, otherwise it is a DROPPED produce; a 'C' succeeds (count -= 1) only if count > 0, otherwise it is a STARVED consume. Return a list [produced, consumed, dropped, starved] tallying how many of each outcome occurred.

Implement
simulate_buffer(ops: list[str], cap: int) → list[int]
Examples
in[["P","P","C","P"],2]out[3,1,0,0]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 40 min
InputExpectedGot
[["P","P","C","P"],2][3,1,0,0]not run yetsample