Implement a Single-Node Priority Pub/Sub
AmazonSoftware Engineer · OnsiteLast updated
This question evaluates understanding of concurrent system design, in-memory publish–subscribe semantics, prioritized delivery ordering, API and data structure selection, and thread-safety within the Software Engineering Fundamentals domain.
Implement a simplified in-memory publish-subscribe system that runs on a single machine.
The system should support users subscribing to topics with a priority.
Requirements
- A user can subscribe to a topic with an integer priority.
- A user can unsubscribe from a topic.
- A publisher can publish a message to a topic.
- When a message is published, it should be delivered to all current subscribers of that topic in descending priority order.
- If two subscribers have the same priority, deliver to the one who subscribed earlier first.
- The implementation should be thread-safe enough to support concurrent subscribe, unsubscribe, and publish calls on a single process.
Design the classes, main APIs, data structures, and explain the trade-offs.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.