Every Check yourself and How interviews ask it question in the wiki, in one place. Say the answer out loud
or write it down before opening it: testing yourself is what makes knowledge stick. 44 questions so far. This page is generated from
the notes, so it grows every day.
Prefer flashcards?
Open the Quiz: the same questions as cards, scheduled with spaced repetition so each one comes
back just before you would forget it. Your progress stays in your browser.
“JavaScript is single-threaded. So how can it do several things at the same time?”
The JavaScript engine runs my code on one thread, but the environment around it (the browser or Node) is not
single-threaded. Timers, network requests and file reads are done by the environment. When one finishes, its
callback goes into a queue, and the event loop runs it when the call stack is empty. Promise callbacks go into a
separate microtask queue that is fully emptied after each task, so they run before timers. So JavaScript is
concurrent (many things in progress) without being parallel (two pieces of my code running at the same moment),
unless I use Web Workers or worker threads.
5, 2, 4, 1, 3. 5 is synchronous. Then the microtasks: 2 (which schedules timer 3 behind timer 1), then 4. Then
the tasks in order: 1, then 3.
”Write a sleep function and use it.”
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));async function demo() { console.log('wait...'); await sleep(1000); console.log('one second later');}demo();
The usual follow-up: “Does sleep block the page?” No. await gives the thread back; the rest of demo comes back
as a microtask after the timer task resolves the promise.
After a script finishes, which runs first: a setTimeout(fn, 0) callback or a .then callback? Why?
The .then callback. It is a microtask, and all microtasks run as soon as the current task (the script) ends,
before the loop takes the next task, which is where the timer callback waits.
Why can an endless chain of promise callbacks freeze a page, while an endless chain of setTimeout calls does not?
The microtask queue must be completely empty before the browser can paint or take the next task. A promise chain
keeps refilling it, so that never happens. Each setTimeout callback is a separate task, and the browser can paint
and handle input between tasks.
When you call an async function, which part of its body runs immediately?
Everything up to the first await. At that point the function returns a pending promise to the caller, and the rest
runs later as a microtask, once the awaited value is ready.
.then callbacks are microtasks, and the microtask queue is emptied right after the current task; the timer callback is a separate task that waits for a later turn.
What happens if microtasks keep adding microtasks forever?
The queue never empties, so the browser never renders or handles input: the page freezes.
Neither exactly. Python passes a reference to the object, and the parameter becomes a new name bound to that same
object. If the function mutates the object, for example appending to a list, the caller sees it. If the function
rebinds the parameter with =, only the local name moves and the caller is not affected. With immutable objects
like ints and strings you cannot mutate, so it looks like pass by value. People call this pass by object reference,
or pass by assignment.
1 2 2. The default set() was created once, when the function was defined, so all three calls share it. Fix:
default to None and create a new set inside the function when the argument is None.
”Hard mode: what happens here?”
t = ([1], 2)t[0] += [3]
It raises TypeError: 'tuple' object does not support item assignment, andt is now ([1, 3], 2).
t[0] += [3] runs in two steps: first the list’s in-place add mutates the list (this works), then Python tries to
store the result back into t[0] (this fails, because tuples cannot be assigned to). It shows exactly the difference
between mutating an object and rebinding a slot.
What is the difference between a == b and a is b, and when should you use is?
== compares values (it calls __eq__); is checks whether both names point at the very same object. Use is
only for singletons such as None, True and False; use == for everything else.
Why does def f(x, items=[]) keep items from earlier calls, and what is the fix?
Default values are evaluated once, when the def line runs, and the same list object is reused on every call, so
appends pile up. Fix: use items=None and create a new list inside the function when items is None.
You copy a list of lists with new = old.copy() and then change new[0][0]. Does old change? Why?
Yes. .copy() is shallow: it makes a new outer list holding the same inner lists, so new[0] and old[0] are the
same object. Use copy.deepcopy(old) to copy the inner lists too.
Why do microservices make “one transaction across everything” hard?
Each service owns its own database, so there is no single database transaction that covers all of them; you need patterns like sagas or events instead.
Name one good reason to split a service out of a monolith.
One part needs to scale very differently (for example search under heavy load), or a separate team needs to deploy it on its own schedule.
Is a Node.js server handling 1,000 open requests concurrent, parallel, or both?
Concurrent. Its JavaScript runs on one thread; the requests are in progress together but don’t execute at the same instant (unless you add worker threads or more processes).
Your program spends most of its time waiting for an API. Concurrency or parallelism?
Concurrency: overlap the waiting. More cores won’t help code that is idle.