Pure Storage Interview Coding Question: Callback Registration and Event Dispatch

15 Views
No Comments

Implement a simple event system with two operations.

public interface Callback {void run();
}

class Event {void registerCallback(Callback cb) {// fill this in}

    void eventFired() {// fill this in}
}

Callbacks can be registered before or after the event is fired. If the event has not fired yet, registered callbacks should be stored and run later when eventFired() is called. If the event has already fired, a newly registered callback should run immediately. Any callback should run at most once.

This problem asks you to implement a small callback dispatcher with lazy execution. Before the event fires, callbacks must be stored and invoked later when <code>eventFired()</code> is called. After the event has already fired, any newly registered callback should execute immediately. The core of the solution is a boolean state flag plus a collection for pending callbacks, with careful handling to ensure each callback runs at most once.

END
 0