Implement an In-Memory Message Broker with Per-Subscriber Read Offsets
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design and implement a simplified in-memory message broker that supports multiple topics, publishers and subscribers.
The broker lets callers create topics, publish messages to topics, and subscribe consumers to topics. Each subscriber keeps an independent read offset per topic, so subscribers consume messages at their own pace without affecting each other.
A subscriber only receives messages that were published after its subscription was created. Polling returns the next unread message for a given subscriber on a given topic, or an empty string if there are no unread messages.
Implement a `MessageBroker` class with these methods:
- `create_topic(topic_name: str) -> bool` creates a topic with the given name. It returns `True` if the topic was created, or `False` if a topic with that name already exists.
- `subscribe(subscriber_id: str, topic_name: str) -> bool` subscribes a consumer to a topic. The subscriber only sees messages published after subscribing. It returns `True` if newly subscribed, or `False` if the topic does not exist or the subscriber is already subscribed to it.
- `publish(topic_name: str, message: str) -> bool` publishes a message to the topic. It returns `True` if published, or `False` if the topic does not exist.
- `poll(subscriber_id: str, topic_name: str) -> str` returns the next unread message for the subscriber on the topic, so the following poll returns the message after it. It returns an empty string if there are no unread messages or the subscriber is not subscribed to the topic.
- `get_unread_count(subscriber_id: str, topic_name: str) -> int` returns the number of unread messages for the subscriber on the topic, or `-1` if the subscriber is not subscribed to the topic.
Example 1, one topic read by two subscribers:
```text
create_topic("orders") -> True
create_topic("orders") -> False
publish("orders", "o1") -> True
subscribe("alice", "orders") -> True
publish("orders", "o2") -> True
publish("orders", "o3") -> True
subscribe("bob", "orders") -> True
publish("orders", "o4") -> True
get_unread_count("alice", "orders") -> 3
poll("alice", "orders") -> "o2"
get_unread_count("bob", "orders") -> 1
poll("bob", "orders") -> "o4"
poll("bob", "orders") -> ""
get_unread_count("alice", "orders") -> 2
```
Alice subscribed after "o1" was published, so she never sees it and her first poll returns "o2". Bob subscribed after "o3", so only "o4" is unread for him and his second poll finds nothing. Bob's reads do not move Alice's position.
Example 2, missing topics and repeated subscriptions:
```text
subscribe("carol", "payments") -> False
publish("payments", "p1") -> False
poll("carol", "payments") -> ""
get_unread_count("carol", "payments") -> -1
create_topic("payments") -> True
subscribe("carol", "payments") -> True
subscribe("carol", "payments") -> False
get_unread_count("carol", "payments") -> 0
poll("carol", "alerts") -> ""
get_unread_count("carol", "alerts") -> -1
```
Before "payments" exists, subscribing and publishing both fail and Carol has no subscription, so the count is `-1`. After the topic is created, her first subscription succeeds, the repeat returns `False`, and she has 0 unread messages. She never subscribed to "alerts", so poll returns an empty string and the count is `-1`.
```hint Per-subscriber state
Ask what is the smallest piece of state each (subscriber, topic) pair needs so that both `poll` and `get_unread_count` run in constant time.
```
```hint The subscribe moment
The rule "only messages published after subscribing" can be settled entirely inside `subscribe`. Think about what you could record at that moment.
```
### Constraints & Assumptions
- `1 <= topic_name.length <= 50`
- `1 <= subscriber_id.length <= 50`
- `1 <= message.length <= 200`
- At most 10,000 total operations.
- Topic names, subscriber IDs and messages consist of alphanumeric characters, spaces and basic punctuation.
- The API has no operation to delete a topic or to unsubscribe.
### Clarifying Questions to Ask
- Is the broker called from a single thread, or must concurrent publishes and polls be safe?
- Can every published message stay in memory at this scale, or should messages that all subscribers have read be freed?
- Is a subscriber ID created implicitly by its first `subscribe` call, or must subscribers be registered separately?
### What a Strong Answer Covers
- A data model for topic messages and per-subscriber read progress that keeps subscribers independent of each other.
- How `subscribe` enforces the rule that only later messages are visible, and how `poll` advances a subscriber's position.
- The exact return value for every failure case: duplicate topic, missing topic, repeated subscription, not subscribed, and nothing left to read.
- The time cost of each operation and the total memory used.
### Follow-up Questions
- How would you free memory for messages that every subscriber has already read, without breaking the offsets?
- How would you make the broker safe when many threads publish and poll at once?
- How would you add `unsubscribe`, and what should happen when the same subscriber subscribes again later?
- How would you support a consumer group, where several workers share one position on a topic and split its messages?
Overview: Implement an in-memory message broker with topics, publishers and subscribers, where each subscriber reads a topic at its own offset and only sees messages published after it subscribed. It tests data modeling for independent consumers, constant-time operations and exact handling of failure return values.