PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of event-driven programming and API design in a frontend environment, specifically the ability to manage event listener registration, removal, and invocation while preserving listener ordering and independent event storage.

  • medium
  • Snowflake
  • Coding & Algorithms
  • Frontend Engineer

Implement an event emitter

Company: Snowflake

Role: Frontend Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement an `EventEmitter` class for a frontend environment. The class should support these methods: - `on(eventName, listener)`: register a listener function for an event. - `off(eventName, listener)`: remove one previously registered listener for that event. - `emit(eventName, ...args)`: invoke all listeners registered for that event in registration order, passing through all provided arguments. Requirements: - Multiple listeners may be registered for the same event. - Removing one listener must not affect other listeners. - Emitting an event with no listeners should do nothing. - Listeners for different event names must be stored independently. - The implementation should be suitable for typical frontend usage.

Quick Answer: This question evaluates understanding of event-driven programming and API design in a frontend environment, specifically the ability to manage event listener registration, removal, and invocation while preserving listener ordering and independent event storage.

Implement the core behavior of an EventEmitter class suitable for typical frontend usage. The emitter supports registering listener functions for named events, removing one previously registered listener, and emitting an event to all currently registered listeners in registration order. For testability, listeners are represented by string IDs instead of real functions. When an emit operation would invoke a listener, record that invocation as a list containing the listener ID followed by all emitted arguments.

Constraints

  • 0 <= len(operations) <= 10000
  • eventName, listenerId, and emitted arguments are strings of length 1 to 100
  • Multiple listeners may be registered for the same event and must be invoked in registration order
  • If the same listener is registered multiple times for the same event, each registration counts separately
  • off(eventName, listenerId) removes only the first remaining matching registration for that event and does nothing if no such listener exists
  • Emitting an event with no registered listeners produces no output

Examples

Input: ([['on','click','first'],['on','click','second'],['emit','click','10','20']],)

Expected Output: [['first','10','20'],['second','10','20']]

Explanation: Both click listeners are invoked in registration order with the emitted arguments.

Input: ([['on','click','A'],['on','hover','B'],['on','click','C'],['off','click','A'],['emit','click','x'],['emit','hover','y']],)

Expected Output: [['C','x'],['B','y']]

Explanation: Removing A from click leaves C for click. The hover listener B is stored independently.

Input: ([['emit','ready'],['off','ready','missing'],['on','ready','A'],['off','ready','B'],['emit','ready','ok']],)

Expected Output: [['A','ok']]

Explanation: Emitting with no listeners and removing missing listeners do nothing. A remains registered and is invoked.

Input: ([['on','message','A'],['on','message','A'],['on','message','B'],['off','message','A'],['emit','message','hello'],['off','message','A'],['emit','message','again']],)

Expected Output: [['A','hello'],['B','hello'],['B','again']]

Explanation: A was registered twice. The first off removes only one A, so A and B receive the first emit. The second off removes the remaining A.

Input: ([['on','open','L'],['on','close','L'],['off','open','L'],['emit','open'],['emit','close']],)

Expected Output: [['L']]

Explanation: The same listener ID can be registered for different events independently. The close event emits with no extra arguments.

Input: ([],)

Expected Output: []

Explanation: With no operations, there are no listener invocations.

Hints

  1. Use a dictionary/hash map from event names to an ordered list of listeners.
  2. For off, scan only the listener list for that event and remove the first matching listener ID.
Last updated: Jun 16, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Solve Matrix and Array Distance Problems - Snowflake (medium)
  • Solve Array Distance and Wiki Navigation - Snowflake (medium)
  • Implement Document Predicate APIs - Snowflake (medium)
  • Find Shortest Wiki Click Path - Snowflake (medium)
  • Compute Inherited Role Privileges - Snowflake (hard)