How Node really works · Chapter 33 of 34

Interview: Node internals

By the end of this chapter you can predict what Node runs next, tell code that blocks the event loop from code that does not, and say what the module caches keep. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The interview
  • 6 tasks
  • Node.js
  • In the all labs plan

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.

Why you need this

Tapri is hiring a second backend developer, and Meera wants you on the interview panel. First Dev, the CTO, puts you through the same round. "Nobody cares if you can recite the phases. I want to see if you can predict what a server does under load, and say why."

These are production questions: why a 0 ms timer fired late, why one report froze every order, why a module kept its old state. Guessing from how a snippet looks gets them wrong; the rules from chapters 1, 4 and 5 get them right.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1 · interview question

    Interview: predict the order of nextTick, promises and timers

    Round 1 in the starter is a CommonJS script, main.cjs, with a timer, two promise callbacks, a queueMicrotask and two ticks, where one tick queues a promise and one promise queues a tick. Set microtasks to the lines it prints, in order, each as a string exactly as it appears in the terminal.

    The check runs the same script with node main.cjs in a fresh process and compares your list with its output line by line, so the length must match too.

  2. Task 2 · interview question

    Interview: predict setImmediate and setTimeout inside an I/O callback

    Round 2 is a main.cjs whose code runs inside a readFile callback, with a 0 ms timer, two setImmediate callbacks (the first queues a promise), a tick, a promise and a plain console line. Set insideIo to the lines it prints, in order, as strings exactly as printed.

    The check runs the script with node main.cjs and compares line by line.

  3. Task 3 · interview question

    Interview: predict what recursive nextTick does to timers and I/O

    Round 3 is a main.cjs that starts a file read and a 0 ms timer, then queues a tick that keeps queuing itself until it has run 100000 times, and prints a line with the tick count so far. The file read and the timer also print the tick count when they run. Set starvation to the lines it prints, in order, as strings exactly as printed, with the numbers written as they appear.

    The check runs the script with node main.cjs and compares line by line.

  4. Task 4 · interview question

    Interview: processInBatches, fix the loop that starves the server

    Write processInBatches(items, handle, batchSize), with batchSize 1000 when it is left out. It returns a promise of handle(item) for every item, in the same order, like items.map(handle).

    It works through batchSize items at a time, and after each whole batch it gives the event loop a real turn before going on: a setImmediate callback queued by someone else must run between two batches, so at every turn the number of items handled so far is a whole number of batches. An empty list resolves to an empty array.

  5. Task 5 · interview question

    Interview: blocks, say which calls block the event loop

    Fill blocks with true or false (real booleans, not strings) for each of its six operations, keeping the six keys. true means the operation blocks: the loop cannot run anything else until it has finished. false means the loop gets a turn while it works.

    The check measures each one for real, the way the probe in the lesson does: inside an I/O callback it queues a setImmediate, runs the operation and waits for it to finish, and the operation blocks if that setImmediate had not run by then. The file is a JSON menu of 20,000 items.

  6. Task 6 · interview question

    Interview: predict what require and import cache

    Round 6 is three files: counter.cjs (a counter module that prints a line when it loads), tea.mjs (an ES module that prints a line when it loads and exports an array) and main.cjs, which requires the counter several times, deletes it from require.cache once, and imports tea.mjs twice and tea.mjs?again once. Set caching to the lines node main.cjs prints, in order, as strings exactly as printed: true prints as 'true' and 2 as '2'.

    The check writes the three files to a fresh folder, runs main.cjs and compares line by line.

What you will learn

  • Naming the rule: the queues and phases, said out loud before the answer.
  • "It depends": when Node does not fix an order, and why.
  • A probe for blocking: a few lines that tell you whether the loop got a turn.
  • Cache keys: what require.cache and the ES module map are keyed by.

More in The interview

Build "Interview: Node internals" yourself

Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of How Node really works are free. This one comes with the all labs plan.