Senior · Design · Interlocked covers one operation at a time

How would you maintain a capacity invariant spanning accepted and remaining state?

Think through your answer before opening the explanation.

Runtime & concurrency

Reveal answer and reasoning

Name the invariant and what readers must observe, not merely which individual fields need atomic writes. One option is a private lock protecting both validation and all related updates and reads. Another is one immutable state object published through a validated CAS protocol so readers obtain a coherent snapshot. Compute a valid candidate before publication and distinguish an ordinary rejection from a conflict requiring retry.

Test a deliberately arranged intermediate state in a broken split implementation, then verify the selected protocol with controlled gates and direct expected snapshots. Do not rely only on a brief stress run that may miss the vulnerable interleaving. The wrong design performs two Interlocked operations and assumes every reader sees them as one transaction. Persistent capacity across processes additionally needs a real atomic storage contract and failure policy; local counters cannot establish cluster-wide correctness or durable external effects.

What a strong answer covers

  • Define writer and reader snapshot semantics.
  • Protect the whole invariant with one suitable protocol.
  • Use controlled negative evidence and real storage tests where needed.

Common pitfall

Using two independent atomic field updates while promising one coherent state transition.

Follow-up questions

  1. How would cancellation interact with a prepared candidate?
  2. What would a durable quota store need to guarantee?

Self-review

Choose what to revisit. These marks are your own assessment and are separate from lesson completion.

Primary references

Open lesson and code ↗

← All interview questions