← Snowflake Interview Insights
The core API feels approachable until you hit the mutation-during-iteration problem.
Start by clarifying requirements (e.g., error handling, listener context, once semantics) and then implement a class using a Map or object to store event listeners. Walk through each method, explaining design choices and edge cases, and test with a few examples.
Pro tip: Mention that you'd use a Map for O(1) lookups and handle listener removal during emit by iterating over a copy of the array to avoid skipping listeners.
Ask about expected behavior for edge cases: emitting non-existent events, listener context (this binding), and whether once listeners should be removed before or after invocation.
Choose a Map (or object) to map event names to arrays of listener functions. Explain why Map is preferable for frequent additions/removals and non-string keys.
Write on to add a listener to the array for the event, creating the array if needed. Write off to remove a specific listener, handling cases where the event or listener doesn't exist.
For emit, iterate over a copy of the listeners array and call each with provided arguments. For once, wrap the listener in a function that removes itself before calling the original, then register it with on.
Walk through examples: adding/removing listeners, emitting with multiple arguments, once behavior. Discuss trade-offs like memory leaks if listeners aren't removed, and potential improvements (e.g., error handling, max listeners).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.