FastPrepOne-Shot Callback Event Dispatch
Problem · Concurrency

One-Shot Callback Event Dispatch

Learn this problem
EasyPure Storage logoPure StorageFULLTIMEONSITE INTERVIEW

Problem statement

Simulate a one-shot callback event over the linearized operation list operations. Each operation is one of:

  • ["REGISTER", callbackId]: register one callback occurrence. Before the event fires, retain the occurrence for later execution and emit an empty row. After the event fires, execute that occurrence immediately and emit [callbackId].
  • ["FIRE"]: mark the event as fired, execute every retained callback occurrence in registration order, clear the pending queue, and emit the executed callback IDs as one row.

The input contains exactly one FIRE. Registering the same callbackId more than once creates separate callback occurrences, and each occurrence executes exactly once.

The array order is the chosen linearization order for calls that may have originated concurrently. Return one output row per input operation. The task evaluates the observable one-shot semantics from this linearization; no actual multithreading is required.

Function

dispatchCallbacks(operations: String[][]) → String[][]

Examples

Example 1

operations = [["REGISTER","a"],["REGISTER","b"],["FIRE"],["REGISTER","c"]]return = [[],[],["a","b"],["c"]]

The first two callbacks wait. FIRE executes them in registration order, and the later registration executes immediately.

Example 2

operations = [["FIRE"],["REGISTER","x"],["REGISTER","x"]]return = [[],["x"],["x"]]

No callback is pending when the event fires. Both later registrations execute immediately, including the repeated identifier as a separate occurrence.

Constraints

  • 1 <= operations.length <= 100000.
  • Exactly one operation is FIRE.
  • Every other operation is REGISTER with exactly one callback ID.
  • Each callback ID contains 1 to 30 ASCII letters, digits, or underscores.
  • The total number of callback-ID characters is at most 1000000.

More Pure Storage problems

drafts saved locally
public String[][] dispatchCallbacks(String[][] operations) {
    // Write your code here.
}
operations[["REGISTER","a"],["REGISTER","b"],["FIRE"],["REGISTER","c"]]
expected[[], [], ["a", "b"], ["c"]]
checking account