The Event Broadcaster
A medium Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L4
Problem
Implement an EventEmitter that supports on(event, listener), emit(event, payload), and off(event, listener). Listeners are opaque identifiers (strings in the tests). emit notifies every listener currently subscribed to the event by appending the payload to the result list once per listener (so a 3-listener emit returns [payload, payload, payload]); an emit with no listeners returns []. off removes one matching listener identifier (the first one found if duplicates exist). Then implement a driver event_broadcaster(op_names, op_args) that constructs an EventEmitter when op_names[0] is 'EventEmitter' and dispatches the remaining 'on'/'emit'/'off' ops with their parallel arg lists. Return the list of results in op order: None for the constructor, on, and off; the per-listener payload list for emit.
Summary
Subscribers show up, listen, and sometimes leave.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.