← Questions
Memory usage switcher with configurable thresholds
0
I was asked this
No reports yet. Sign in to say where you were asked it.
An object-oriented design interview question asked in Amazon's technical phone screen, last asked Sep 2025. It tests object-oriented design and state machines.
Design and code an in-memory switcher that watches the process's memory usage and switches between ENABLED and DISABLED when usage crosses thresholds the caller configures. For example, a cache or a feature turns itself off when memory runs high and back on once it has dropped.
Requirements
- Functional: separate upper and lower thresholds (hysteresis), so the state does not flap around one value; thresholds changeable at runtime without a restart;
state()readable cheaply from any thread; callbacks or events when the state changes. - Non-functional: thread-safe; sampling memory must cost little; defined behaviour under sudden spikes, when reading the metric fails, and when several processes or instances run; observable through metrics.
Clarifying questions
- What is measured: the process's resident memory, the heap, the container's usage?
- Should the switch trip on one reading above the threshold, or only after several?
- In which state should it start, and what should it do if the metric cannot be read?
- Is the decision per process, or shared across instances?
What a strong answer covers
- A small state machine:
ENABLEDgoes toDISABLEDwhen usage rises aboveupper;DISABLEDgoes back only when usage falls belowlower;lower < uppervalidated on every reconfiguration. - The API:
MemorySwitcher(upper, lower, sampler, interval),state(),reconfigure(upper, lower),on_change(callback),start()/stop(). - Concurrency: a background sampler thread updates an atomic state; readers never take a lock; thresholds swapped as one immutable config object; callbacks run outside any lock, after the state change.
- Spikes and noise: requiring N consecutive readings, or a moving average, and an emergency path that trips at once above a hard limit.
- Metric failures: keep the last state for a while, then fail safe (usually
DISABLED), and count the failures. - Pull versus push: polling on an interval is simple and bounded; push from memory-pressure notifications reacts faster but depends on the platform.
- Several instances: each decides from its own memory unless the protected resource is shared.
- Tests with a fake sampler and clock: crossing each threshold, staying between them, reconfiguring mid-run, sampler errors.
Follow-ups
- Add a third state that sheds load gradually before disabling.
- How do you choose the thresholds and the sampling interval?
- What metrics and alerts would you add?
Where it's asked
- Amazon · Technical phone screen · Last asked Sep 202560 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
object-oriented design · state machines
No official answer yet. Draw yours on the right; publish it and it shows under Community.
Nothing published yet. Publish your canvas and it shows here for others to open and remix.
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing
