Problem, as an interviewer would state it: "Build a Pub-Sub system — subscribe(topic, handler) and publish(topic, data). Multiple things can subscribe to the same topic, and subscribing should give back a way to unsubscribe later."
Examples:
const unsub = bus.subscribe("user:login", (data) => console.log(data));
bus.publish("user:login", { id: 1 }); // handler runs
unsub();
bus.publish("user:login", { id: 2 }); // handler does NOT run
Clarifying questions expected:
- How does this genuinely differ from a plain EventEmitter — is topic-based routing the key distinction?
- Should publishing to a topic with zero subscribers throw, or be a silent no-op?
- Do handlers need to run in subscription order, or is order unspecified?
Code / implementation expected: Yes — real, direct proof of multiple subscribers per topic, correct unsubscribe behavior, real topic isolation, and a genuine no-op on a topic with no subscribers.