Implement a Two-Priority Message Buffer with Size-Based Eviction and Drain

Quick Overview

Implement a two-priority message buffer with a size-based capacity: push evicts low-priority messages before high-priority ones until the new message fits, and drain removes messages until the drained size passes a limit. It tests policy clarification, size bookkeeping and hand-traced correctness without running code.

Implement a Two-Priority Message Buffer with Size-Based Eviction and Drain

Company: Amazon

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

Implement a priority buffer that holds messages up to a fixed capacity, measured as the total size of the stored messages. Each message has a size and one of two priorities, high or low. The buffer supports two operations: - `push(message)`: add the message. If adding it would exceed the capacity, evict low-priority messages first, then high-priority messages, until there is enough room. - `drain(max_capacity)`: remove messages from a queue and return them, continuing until the total size of the drained messages is greater than `max_capacity`. Code is written live and cannot be run during the round, so you will trace it by hand with the interviewer. ### Constraints and Clarifications - Assume capacity and message sizes are positive integers. - After `push` returns, the stored total never exceeds the capacity. ### Clarifying Questions - Within one priority, which message is evicted first: the oldest or the newest? - May an incoming low-priority message evict high-priority messages to make room, or should it be rejected instead? - What happens when a single message is larger than the whole capacity? - In what order does `drain` return messages: high priority first, or arrival order across both priorities? - Does `drain` include the message that pushes the drained total past `max_capacity`, or stop before it? ### Part 1: Push with eviction Choose the buffer's internal state and implement `push`, stating the policy you chose for each open question about eviction. `push` must never leave the buffer over capacity. ```hint Find the next victim fast Think about which structure gives you the next message to evict in each priority class without scanning the buffer. ``` #### What This Part Should Cover - Data structures for the two priorities and the running total size - A correct eviction loop: order, stopping condition and size bookkeeping - Oversized messages and what `push` reports back to the caller ### Part 2: Drain up to a size limit Implement `drain(max_capacity)`. Pin down the stopping rule and the order across priorities, then trace an example by hand. ```hint Test the exact boundary Build a small example where the limit falls exactly on a message boundary and work out by hand what `drain` should return. ``` #### What This Part Should Cover - The exact stopping rule and its behavior at the boundary - Drain order across the two priorities - A consistent running total after partial drains ### What a Strong Answer Covers - An explicit decision for each open policy, confirmed with the interviewer before coding - Correct size bookkeeping across push, eviction and drain - Hand-traced cases in place of running tests: an exact fit, eviction across both priorities, an oversized message and an empty drain - Time and space complexity of each operation ### Follow-up Questions - How would you make the buffer safe for one producer thread and one consumer thread? - How would the design change with more than two priority levels? - If evicted messages must never be lost silently, what should `push` return or emit?

Overview: Implement a two-priority message buffer with a size-based capacity: push evicts low-priority messages before high-priority ones until the new message fits, and drain removes messages until the drained size passes a limit. It tests policy clarification, size bookkeeping and hand-traced correctness without running code.

Read the full Amazon Software Engineer interview experience this question came from

Amazon logo
Amazon
Nov 25, 2026
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
0
0

Implement a priority buffer that holds messages up to a fixed capacity, measured as the total size of the stored messages. Each message has a size and one of two priorities, high or low.

The buffer supports two operations:

  • push(message) : add the message. If adding it would exceed the capacity, evict low-priority messages first, then high-priority messages, until there is enough room.
  • drain(max_capacity) : remove messages from a queue and return them, continuing until the total size of the drained messages is greater than max_capacity .

Code is written live and cannot be run during the round, so you will trace it by hand with the interviewer.

Constraints and Clarifications

  • Assume capacity and message sizes are positive integers.
  • After push returns, the stored total never exceeds the capacity.

Clarifying Questions Guidance

  • Within one priority, which message is evicted first: the oldest or the newest?
  • May an incoming low-priority message evict high-priority messages to make room, or should it be rejected instead?
  • What happens when a single message is larger than the whole capacity?
  • In what order does drain return messages: high priority first, or arrival order across both priorities?
  • Does drain include the message that pushes the drained total past max_capacity , or stop before it?

Part 1: Push with eviction

Choose the buffer's internal state and implement push, stating the policy you chose for each open question about eviction. push must never leave the buffer over capacity.

What This Part Should Cover Guidance

  • Data structures for the two priorities and the running total size
  • A correct eviction loop: order, stopping condition and size bookkeeping
  • Oversized messages and what push reports back to the caller

Part 2: Drain up to a size limit

Implement drain(max_capacity). Pin down the stopping rule and the order across priorities, then trace an example by hand.

What This Part Should Cover Guidance

  • The exact stopping rule and its behavior at the boundary
  • Drain order across the two priorities
  • A consistent running total after partial drains

What a Strong Answer Covers Guidance

  • An explicit decision for each open policy, confirmed with the interviewer before coding
  • Correct size bookkeeping across push, eviction and drain
  • Hand-traced cases in place of running tests: an exact fit, eviction across both priorities, an oversized message and an empty drain
  • Time and space complexity of each operation

Follow-up Questions Guidance

  • How would you make the buffer safe for one producer thread and one consumer thread?
  • How would the design change with more than two priority levels?
  • If evicted messages must never be lost silently, what should push return or emit?