Middle · Explain · Senior tradeoffs

What does the senior cache policy account for, and why is its byte counter not a complete managed-memory limit?

Think through your answer before opening the explanation.

Coding interviews

Reveal answer and reasoning

A senior interview answer should name the resource budget, admission rule and failure behavior before choosing infrastructure. This policy tracks declared payload byte sizes and rejects updates that would exceed five accounted bytes. Replacing an existing key first subtracts its previous contribution. The program stores size metadata only; it does not allocate payloads or measure managed heap usage. The counter is intentionally named accounted bytes because it sums caller-supplied sizes rather than observing allocations.

A proposal uses a widened intermediate so subtracting the old size and adding the replacement cannot overflow int before comparison with the budget. A rejected proposal leaves both the size map and Used unchanged.

Expected dictionary operations are constant-time, with O(u) metadata for u keys and key-text storage in addition to the stated payload budget. A zero-byte entry can still consume metadata, so a payload-byte limit alone does not bound entry count or actual process memory. Metadata is O(u) even when every declared payload size is zero. An actual implementation could impose both entry and payload limits, account for key length and define whether duplicate updates allocate.

Measuring managed heap bytes would require instrumentation and would still differ from total process resident memory.

What a strong answer covers

  • A senior interview answer should name the resource budget, admission rule and failure behavior before choosing infrastructure.
  • The rejected b insertion tests nonmutation under pressure, while the a replacement tests subtract-before-add accounting.
  • Expected dictionary operations are constant-time, with O(u) metadata for u keys and key-text storage in addition to the stated payload budget.

Common pitfall

Calling the counter a memory limit would overstate what it measures. It excludes keys, objects, allocator overhead and concurrency. A compound update also needs synchronization under shared access. The implementation chooses reject rather than eviction, expiration or persistence; those policies are deliberately left for the design discussion.

Follow-up questions

  1. Test an oversized replacement of a and verify its previous size remains counted. Add many zero-byte distinct keys and explain why Used stays constant while metadata grows. Hint: a budget is meaningful only relative to the resource actually counted.
  2. Why can arbitrarily many zero-byte keys defeat a payload-only budget?

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