← Questions

In-memory pub/sub with priorities

0
AmazonOO designLast asked Apr 2026

An object-oriented design interview question asked in Amazon's technical phone screen, last asked Apr 2026. It tests object-oriented design, pub/sub and priority queues.

Design and code a publish/subscribe system that runs in one process. Publishers send messages to named topics; subscribers subscribe to and unsubscribe from topics; every current subscriber of a topic receives each message published to it. Then extend it: messages carry a priority, and higher-priority messages are delivered first.

Requirements

  • Many topics and many subscribers; a subscriber can follow several topics.
  • Publishing delivers to all current subscribers of that topic; unsubscribed subscribers stop receiving.
  • Follow-up: each message has a priority, and pending messages are delivered highest priority first, oldest first within a priority.
  • Safe when several threads publish and subscribe at once.

Classes and operations

Unless you agree on a different design with the interviewer, assume:

  • Broker.subscribe(topic: str, subscriber: Subscriber) -> None, unsubscribe(topic, subscriber) -> None
  • Broker.publish(topic: str, payload, priority: int = 0) -> None
  • Subscriber.on_message(topic: str, message: Message) -> None
  • Message(id, topic, payload, priority, published_at)

Example

broker.subscribe("orders", alice)
broker.subscribe("orders", bob)
broker.publish("orders", "o-1")        # alice and bob get o-1
broker.unsubscribe("orders", bob)
broker.publish("orders", "o-2")        # only alice gets o-2

# with priorities and a delivery queue, while delivery is paused:
publish("alerts", "low",  priority=1)
publish("alerts", "high", priority=9)
publish("alerts", "mid",  priority=5)
resume                                  # delivered: high, mid, low

Clarifying questions

  • Synchronous delivery in publish, or a queue and worker threads?
  • Delivery guarantees: at most once, at least once?
  • Does a subscriber that joins late see earlier messages?
  • What happens when a subscriber's handler throws or is slow?

What a strong answer covers

  • Clear classes: broker, topic (with its subscriber set), subscriber interface, message.
  • Synchronous delivery first, then asynchronous delivery with a queue per subscriber so one slow subscriber doesn't block the rest.
  • The priority follow-up: a priority queue keyed by (-priority, sequence number) so equal priorities stay in order; starvation of low priorities and how to bound it.
  • Thread safety: copy the subscriber list under a lock (or use a concurrent set) before delivering; no lock held while calling handlers.
  • How retries, persistence and dead-letter handling would fit.

Follow-ups

  • Wildcard topics (orders.*).
  • Retries with backoff for a failing subscriber, and a dead-letter queue.
  • Persist messages so a restarted subscriber can catch up.

Where it's asked

  • Amazon · Technical phone screen · Last asked Apr 202660 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI

What it tests

object-oriented design · pub/sub · priority queues

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