JavaScript, inside out · Chapter 50 of 51
Event emitter, LRU cache and curry
By the end of this chapter you can build three small machines that interviewers love: an event emitter, an LRU cache and curry. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The interview
- 5 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.
Why you need this
Round five is the machine round. "When an order is placed," Dev says, "the kitchen screen, the rider app and Anjali's SMS service all need to know. The menu lookups are slow, so we keep the hot ones in memory, but memory is limited. And Kabir's pricing helpers take their arguments one at a time. Build me the three pieces."
With what you know so far, the order code would call the kitchen, the rider app and the SMS service by name, and every new service would mean editing it again. A plain object as a cache grows until memory runs out. And a helper that remembers its first arguments breaks the second time you reuse it, if every call writes into the same array. Each piece is short; the edge cases are what Dev pokes at.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1 · interview question
Interview: EventEmitter, on and emit
Fill in on(event, listener) and emit(event, ...args) in the class EventEmitter. on adds a listener for an event and returns the emitter itself, so calls chain. emit calls every listener of that event, in the order they were added, with all of emit's arguments after the event name, and returns true. With no listeners for that event, emit does nothing and returns false.
Each event has its own listeners, and each emitter has its own (two emitters never share). The same function added twice runs twice. A listener added during an emit is not called in that emit, only in the next one.
- Task 2 · interview question
Interview: EventEmitter, off removes one listener
Fill in off(event, listener). It removes one registration of that listener for that event and returns the emitter. A function added twice and removed once still runs once. Removing a listener that is not there, or from an event nobody listens to, does nothing and does not throw.
Once an event has no listeners left, emit returns false for it. A listener that removes itself during an emit must not make the next listener get skipped: every listener that was there when the emit started runs, in order.
- Task 3 · interview question
Interview: EventEmitter, once fires a single time
Fill in once(event, listener). The listener runs on the next emit of that event only, with emit's arguments, and then is gone. once returns the emitter.
A once listener keeps its place in the order among the other listeners, and two once listeners in a row both fire, in order, on the same emit. off(event, listener) with the original function removes a once listener before it fires. After the last listener of an event has fired and gone, emit returns false for it.
- Task 4 · interview question
Interview: LRUCache, evict the least recently used
Fill in the class LRUCache. new LRUCache(capacity) holds at most capacity entries. get(key) returns the value, or -1 when the key is missing. put(key, value) adds the key, or updates it if it is already there.
Both get and put count as using a key. When a put adds a new key and the cache is over capacity, remove the entry used longest ago. Updating an existing key makes it the most recent and removes nothing. Keys can be anything, objects too, and falsy values (0, the empty string, null) come back as they are. Each cache has its own entries. Both operations should be fast, without searching a list.
- Task 5 · interview question
Interview: curry, arguments in groups of any size
Write curry(fn). It returns a function that collects arguments over any number of calls, in groups of any size. While it has fewer than fn.length arguments, each call returns a new function. As soon as it has fn.length or more, it calls fn with all of them (extra ones included) and returns fn's result.
So with bill = curry((price, qty, tip) => price * qty + tip), bill(40)(2)(5), bill(40, 2)(5), bill(40)(2, 5) and bill(40, 2, 5) all give 85. A partial like bill(40) can be reused any number of times, and calls through one partial never affect another. fn runs only once it has enough arguments, and a function with no parameters runs on the first call.
What you will learn
- Changing a list while you loop over it: why the next item gets skipped, and how a copy fixes it.
- A wrapper that remembers the original: so you can still find it.
- A Map's order: the first key is the oldest, and delete then set moves a key to the end.
- Collecting arguments: fn.length, and partial calls that do not share state.
More in The interview
Build "Event emitter, LRU cache and curry" yourself
Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.