LearnFrenzy
Sign in Take a free test

Event Loop · Exercise 4 of 4

Event Loop Event Loop Coding Problems

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Event-loop programs asked in interviews: a mini event-loop simulator, chunked processing, batched updates, a timer scheduler with a virtual clock, sleep sort, a recursive-setTimeout interval, a cooperative scheduler, an execution-order quiz generator, a message queue drained per tick and a promise-based task runner. Write each one yourself first, then open the solution – every solution was run on Node.js and the output shown is the real output.

Question 1 Hard

Build a tiny model of the event loop with a call stack-free "run" method, a task queue and a microtask queue: after each task, drain all microtasks. Use it to replay a typical interview snippet.

Question page & discussion
Question 2 Medium

Process 10 records in chunks of 4, yielding to the event loop between chunks with setTimeout, and show that a "click" task scheduled at the start runs between chunks.

Question page & discussion
Question 3 Medium

Write a small store whose set(key, value) can be called many times in a row but triggers only one render per synchronous turn, listing the keys that changed.

Question page & discussion
Question 4 Hard

Write a FakeTimers class (as testing libraries do) with setTimeout(fn, ms), clearTimeout(id) and tick(ms): timers fire in due-time order, equal times in creation order, and a timer may schedule another one.

Question page & discussion
Question 5 Easy

Implement the (joke) "sleep sort": schedule each number with a delay proportional to its value and collect them as the timers fire. Explain why it is not a real sorting algorithm.

Question page & discussion
Question 6 Medium

Write every(ms, fn, times) using recursive setTimeout instead of setInterval: it runs fn the given number of times, never overlapping, and returns a stop function.

Question page & discussion
Question 7 Hard

Each task is a generator that yields when it wants to give up the CPU. Write a scheduler that runs the tasks round-robin, one step each, until all are finished – the idea behind the single-threaded event loop.

Question page & discussion
Question 8 Medium

Write a helper that numbers every callback as it runs and prints the final order with each callback's queue, for a snippet mixing sync code, a timer, a promise chain and an async function.

Question page & discussion
Question 9 Medium

Notifications arrive faster than they can be shown. Write a queue that shows at most 2 messages per tick using setInterval, stops the interval when the queue is empty, and accepts new messages while running.

Question page & discussion
Question 10 Medium

Write runTasks(tasks) that runs a list of synchronous jobs one per macrotask (yielding with a setTimeout promise between them) and resolves with their results; show that a promise callback queued at the start runs before the first job, and a timer between jobs.

Question page & discussion
All exercises doneBack to Event LoopRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links