Code Room
CodingHardcod-g450
Subject Concurrency simulationLevel Senior–Staff~40 minCommon in Concurrency interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.