Design a message processing system that consumes messages in timestamp order

Quick Overview

Design a class that buffers messages from many producers and returns them in timestamp order, breaking ties by message content. It tests choosing a structure with fast insert and remove-minimum, getting the tie-break right, handling an empty buffer, and stating the cost of each method.

Design a message processing system that consumes messages in timestamp order

Company: X (Twitter)

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a message processing system that accepts messages from multiple producers, each tagged with a timestamp, and processes them in strict timestamp order. The system buffers incoming messages and lets a caller consume them one at a time or all at once, always sorted by timestamp. When two messages share the same timestamp, they are ordered lexicographically by their content. The system must support producing messages, consuming the next message, peeking at the next message without removing it, and querying the buffer size. Implement a class `MessageProcessingSystem` with these methods: - `produce(timestamp: int, content: str)`: add a message with the given timestamp and content to the buffer. - `consume() -> str`: remove and return the next message in timestamp order, formatted as `"timestamp:content"`. Return `""` if the buffer is empty. - `peek() -> str`: return the next message in timestamp order, in the same format, without removing it. Return `""` if the buffer is empty. - `size() -> int`: return the number of messages currently in the buffer. - `isEmpty() -> bool`: return `True` if the buffer has no messages, `False` otherwise. - `consumeAll() -> List[str]`: consume all buffered messages in timestamp order and return them as a list of `"timestamp:content"` strings. ```hint Picking the buffer Messages arrive in any order but must leave smallest first, and there can be up to 100,000 calls. Re-sorting the buffer on every call is too slow. Which structure keeps the smallest element cheap to read and remove after each insert? ``` ```hint Ordering key Equal timestamps are broken by content. Check how your language compares pairs, and keep the timestamp as an integer until you format the output: as strings, "10" sorts before "9". ``` ### Examples #### Example 1 ```text Input: ["MessageProcessingSystem", "produce", "produce", "produce", "peek", "size", "consume", "consume", "consume", "isEmpty"] [[], [3, "msgc"], [1, "msga"], [2, "msgb"], [], [], [], [], [], []] Output: [null, null, null, null, "1:msga", 3, "1:msga", "2:msgb", "3:msgc", true] ``` ```python mps = MessageProcessingSystem() # empty buffer mps.produce(3, "msgc") # buffer: [(3, msgc)] mps.produce(1, "msga") # buffer: [(1, msga), (3, msgc)] mps.produce(2, "msgb") # buffer: [(1, msga), (2, msgb), (3, msgc)] mps.peek() # "1:msga" (earliest timestamp) mps.size() # 3 mps.consume() # "1:msga" (removes the earliest) mps.consume() # "2:msgb" (next earliest) mps.consume() # "3:msgc" (last message) mps.isEmpty() # True (buffer is now empty) ``` #### Example 2 ```text Input: ["MessageProcessingSystem", "produce", "produce", "produce", "consumeAll", "consume", "isEmpty"] [[], [5, "beta"], [5, "alpha"], [1, "gamma"], [], [], []] Output: [null, null, null, null, ["1:gamma", "5:alpha", "5:beta"], "", true] ``` ```python mps = MessageProcessingSystem() # empty buffer mps.produce(5, "beta") # buffer: [(5, beta)] mps.produce(5, "alpha") # buffer: [(5, alpha), (5, beta)] mps.produce(1, "gamma") # buffer: [(1, gamma), (5, alpha), (5, beta)] mps.consumeAll() # ["1:gamma", "5:alpha", "5:beta"] (all, in order) mps.consume() # "" (buffer is empty) mps.isEmpty() # True ``` ### Constraints & Assumptions - `1 <= total number of messages <= 10^5` - `0 <= timestamp <= 10^9` - Message content is a non-empty string of lowercase English letters and digits. - At most `10^5` calls in total across all methods. ### Clarifying Questions to Ask - Can two messages have the same timestamp and the same content, and should both stay in the buffer? - Does "lexicographic" mean plain character-code comparison, so digits sort before letters? - Do producers and consumers call the class from several threads at once, or is a single-threaded class enough for this round? - Once a message is consumed, does anything need to keep it (for replay or auditing), or can it be dropped? ### What a Strong Answer Covers - A buffer whose insert and remove-minimum are both logarithmic, and why a plain list or a sorted array is slower at this scale. - An ordering on timestamp then content that applies the tie-break correctly and compares timestamps as integers. - The empty-buffer cases for `consume` and `peek`, and duplicate messages kept as separate entries. - The time cost of every method, including `consumeAll`, and space proportional to the messages still buffered. - How the class would change when producers and consumers run concurrently. ### Follow-up Questions - Producers run on their own threads and a consumer should block until a message is available. What changes in the class? - A producer can deliver a message late, after a message with a later timestamp has already been consumed. How would you keep strict ordering across producers? - The buffer has a fixed capacity. What should `produce` do when it is full? - How would you add `consumeUntil(timestamp)`, which returns every message up to and including a given timestamp?

Overview: Design a class that buffers messages from many producers and returns them in timestamp order, breaking ties by message content. It tests choosing a structure with fast insert and remove-minimum, getting the tie-break right, handling an empty buffer, and stating the cost of each method.

X (Twitter) logo
X (Twitter)
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a message processing system that accepts messages from multiple producers, each tagged with a timestamp, and processes them in strict timestamp order.

The system buffers incoming messages and lets a caller consume them one at a time or all at once, always sorted by timestamp. When two messages share the same timestamp, they are ordered lexicographically by their content.

The system must support producing messages, consuming the next message, peeking at the next message without removing it, and querying the buffer size. Implement a class MessageProcessingSystem with these methods:

  • produce(timestamp: int, content: str) : add a message with the given timestamp and content to the buffer.
  • consume() -> str : remove and return the next message in timestamp order, formatted as "timestamp:content" . Return "" if the buffer is empty.
  • peek() -> str : return the next message in timestamp order, in the same format, without removing it. Return "" if the buffer is empty.
  • size() -> int : return the number of messages currently in the buffer.
  • isEmpty() -> bool : return True if the buffer has no messages, False otherwise.
  • consumeAll() -> List[str] : consume all buffered messages in timestamp order and return them as a list of "timestamp:content" strings.

Examples

Example 1

Input:
["MessageProcessingSystem", "produce", "produce", "produce", "peek", "size", "consume", "consume", "consume", "isEmpty"]
[[], [3, "msgc"], [1, "msga"], [2, "msgb"], [], [], [], [], [], []]

Output:
[null, null, null, null, "1:msga", 3, "1:msga", "2:msgb", "3:msgc", true]
mps = MessageProcessingSystem()   # empty buffer
mps.produce(3, "msgc")            # buffer: [(3, msgc)]
mps.produce(1, "msga")            # buffer: [(1, msga), (3, msgc)]
mps.produce(2, "msgb")            # buffer: [(1, msga), (2, msgb), (3, msgc)]
mps.peek()                        # "1:msga" (earliest timestamp)
mps.size()                        # 3
mps.consume()                     # "1:msga" (removes the earliest)
mps.consume()                     # "2:msgb" (next earliest)
mps.consume()                     # "3:msgc" (last message)
mps.isEmpty()                     # True (buffer is now empty)

Example 2

Input:
["MessageProcessingSystem", "produce", "produce", "produce", "consumeAll", "consume", "isEmpty"]
[[], [5, "beta"], [5, "alpha"], [1, "gamma"], [], [], []]

Output:
[null, null, null, null, ["1:gamma", "5:alpha", "5:beta"], "", true]
mps = MessageProcessingSystem()   # empty buffer
mps.produce(5, "beta")            # buffer: [(5, beta)]
mps.produce(5, "alpha")           # buffer: [(5, alpha), (5, beta)]
mps.produce(1, "gamma")           # buffer: [(1, gamma), (5, alpha), (5, beta)]
mps.consumeAll()                  # ["1:gamma", "5:alpha", "5:beta"] (all, in order)
mps.consume()                     # "" (buffer is empty)
mps.isEmpty()                     # True

Constraints & Assumptions

  • 1 <= total number of messages <= 10^5
  • 0 <= timestamp <= 10^9
  • Message content is a non-empty string of lowercase English letters and digits.
  • At most 10^5 calls in total across all methods.

Clarifying Questions to Ask Guidance

  • Can two messages have the same timestamp and the same content, and should both stay in the buffer?
  • Does "lexicographic" mean plain character-code comparison, so digits sort before letters?
  • Do producers and consumers call the class from several threads at once, or is a single-threaded class enough for this round?
  • Once a message is consumed, does anything need to keep it (for replay or auditing), or can it be dropped?

What a Strong Answer Covers Guidance

  • A buffer whose insert and remove-minimum are both logarithmic, and why a plain list or a sorted array is slower at this scale.
  • An ordering on timestamp then content that applies the tie-break correctly and compares timestamps as integers.
  • The empty-buffer cases for consume and peek , and duplicate messages kept as separate entries.
  • The time cost of every method, including consumeAll , and space proportional to the messages still buffered.
  • How the class would change when producers and consumers run concurrently.

Follow-up Questions Guidance

  • Producers run on their own threads and a consumer should block until a message is available. What changes in the class?
  • A producer can deliver a message late, after a message with a later timestamp has already been consumed. How would you keep strict ordering across producers?
  • The buffer has a fixed capacity. What should produce do when it is full?
  • How would you add consumeUntil(timestamp) , which returns every message up to and including a given timestamp?