System Design Quiz Practice
Active Recall Drill Session
← Exit SessionQuestion 1 of 1
hardSystem Design
Given this simplified pub/sub simulation, does Server1 or Server2 print the 'delivering' message when userA (on Server1) messages userB (on Server2)?
code
class SharedBus {
constructor() { this.subs = []; }
subscribe(s) { this.subs.push(s); }
publish(msg) { for (const s of this.subs) s.handleBusMessage(msg); }
}
class Server {
constructor(name, bus) { this.name = name; this.conns = new Map(); bus.subscribe(this); }
connect(id) { this.conns.set(id, true); }
send(to, text) { console.log(`from ${this.name}`); this.bus?.publish({ to, text }); }
handleBusMessage({ to, text }) {
if (this.conns.has(to)) console.log(`${this.name}: delivering "${text}" to ${to}`);
}
}
const bus = new SharedBus();
const s1 = new Server("Server1", bus); s1.bus = bus;
const s2 = new Server("Server2", bus); s2.bus = bus;
s1.connect("userA"); s2.connect("userB");
s1.send("userB", "hi");