← Questions

Implement an event emitter

0
SnowflakeCodingLast asked Jan 2026

A coding interview question asked in Snowflake's two coding screens round, last asked Jan 2026. It tests javascript, frontend and api design.

Implement a small event emitter for a frontend codebase, like the one in Node or in many UI libraries. Code subscribes handlers to named events and other code emits those events with arguments. Each event name keeps its own list of handlers, and handlers run in the order they were added.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • on(event: string, handler: Function) -> () => void registers the handler and returns a function that unsubscribes exactly this registration.
  • off(event: string, handler: Function) -> void removes one registration of that handler; a handler that is not registered is ignored.
  • once(event: string, handler: Function) -> () => void registers a handler that runs at most once and then removes itself.
  • emit(event: string, ...args) -> boolean calls every handler of that event with args, in registration order, and returns whether any handler ran.

Example

const e = new EventEmitter();
const off = e.on("save", (x) => log("A", x));
e.once("save", (x) => log("B", x));
e.emit("save", 1);     // logs A 1, then B 1; returns true
e.emit("save", 2);     // logs A 2; returns true
off();
e.emit("save", 3);     // nothing runs; returns false
e.emit("load");        // no handlers; returns false

Constraints

  • The same function may be registered several times; each registration is separate.
  • Emitting one event never calls handlers of another event.

Clarifying questions

  • If a handler is registered twice and off is called once, are both removed or only one?
  • If a handler removes another handler (or itself) during emit, does the removed one still run in this emit?
  • If a handler adds a new handler during emit, does the new one run in this emit?
  • What happens when a handler throws: do the remaining handlers still run?
  • Should off with a function passed to once remove that once-registration?

What a strong answer covers

  • A map from event name to an ordered array of registrations, and why arrays (or a linked list) keep order.
  • Snapshotting the handler list at the start of emit, so handlers that add or remove handlers during the loop do not corrupt the iteration; a stated rule for what removed and added handlers see.
  • once implemented as a wrapper that unsubscribes itself before calling the original, and off(original) still finding the wrapper.
  • Error handling in emit and cleanup of empty event entries.
  • Complexity per operation and tests for ordering, duplicates, once, and mutation during emit.

Follow-ups

  • Add a wildcard listener that receives every event.
  • Warn when more than N handlers are attached to one event (a leak check).
  • Make emit support async handlers and return a promise that settles when they all finish.

Where it's asked

  • Snowflake · Two coding screens · Last asked Jan 202660 min · Two back-to-back live coding rounds, often on the same day; some loops pair coding with system design · Varies: some screens forbid AI, some 2026 screens allow it. The scheduling email says.

What it tests

javascript · frontend · api design

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding