← Questions

Memory usage switcher with configurable thresholds

0
AmazonOO designLast asked Sep 2025

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: ENABLED goes to DISABLED when usage rises above upper; DISABLED goes back only when usage falls below lower; lower < upper validated 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

Your canvas
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing