Code Room
CodingMediumcod-g1011
Subject Concurrency semaphore permit simulationLevel Mid–Senior~25 minCommon in Concurrency interviewsIndustries Software development, Technology

Question

Simulate a counting semaphore that starts with `permits` available permits and a maximum cap of `permits` (it can never exceed its initial value). You are given a list of ops, each a string "A" (acquire one permit) or "R" (release one permit). An "A" succeeds and is granted only if at least one permit is available, decrementing the count; otherwise it is rejected. An "R" returns a permit, incrementing the count but never above the cap (an over-release is ignored, leaving the count unchanged). Process ops in order and return a pair [granted, final_available] where granted is the number of successful acquires and final_available is the permits remaining at the end.

Implement
simulate_semaphore(permits: int, ops: list[str]) → list[int]
Examples
in[2,["A","A","A","R","A"]]out[3,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.