Interview prep
Interview prep for JavaScript, React and Next.js: solve event loop output puzzles with a simple formula, keep one-line answers for the most asked React and Next.js questions, and follow a clear approach in the machine coding round.
75.1 Output puzzle: the print order
console.log("A");setTimeout(() => console.log("B"), 0);Promise.resolve() .then(() => console.log("C"));(async () => { console.log("D"); await null; console.log("E");})();console.log("F");> A> D> F> C> E> B- Many people answer in code order; the real output is A D F C E B.
75.2 The formula: sync, microtasks, macrotasks
- Step 1
1.1. Sync code line by line: A
- Step 2
2.Async function runs like a normal function until await: D, then F
- Step 3
3.2. Call stack empty -> microtask queue: then -> C, code after await -> E
- Step 4
4.3. Macrotasks last: setTimeout callback -> B
Even with delay 0, setTimeout runs last
| Queue | Examples | When it runs |
|---|---|---|
| Call stack (sync) | console.log, async fn body before await | First, line by line |
| Microtasks | Promise .then, code after await | As soon as the stack is empty, all of them |
| Macrotasks | setTimeout, setInterval | After microtasks, one at a time |
75.3 Checkpoint: var in a loop
for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0);}> 3> 3> 3Screen: with let: 0, 1, 2- var is function scoped: all three callbacks share the same i.
- By the time the callbacks run, the loop has finished and i is 3.
- With let, every iteration gets its own new i, so the output is 0, 1, 2.
75.4 React: most asked questions
| Question | One line answer |
|---|---|
| Virtual DOM + reconciliation | React compares the new UI with the previous one and updates only the changed parts in the real DOM. |
| Why keys? | So React can identify every list item; index as key causes bugs when items are reordered. |
| useEffect | For side effects; the dependency array decides when it re-runs, cleanup removes the old timer/subscription. |
| useMemo / useCallback | Only when you measure real slowness; the React Compiler can also do it automatically. |
75.5 Common mistake: stale closure
useEffect(() => { const id = setInterval(() => { setSeconds(seconds + 1); }, 1000); return () => clearInterval(id);}, []);useEffect(() => { const id = setInterval(() => { setSeconds((s) => s + 1); }, 1000); return () => clearInterval(id);}, []);- Wrong (first): the screen gets stuck at "Seconds: 1".
- Reason: the effect ran only on the first render, so the callback always sees seconds = 0 (stale closure).
- Right (second): the updater function (s) => s + 1 uses the previous value, so it moves +1 every second.
75.6 Next.js: most asked questions
- Server vs Client Components: everything is a server component by default; add "use client" only for state, events or browser APIs.
- SSR vs SSG vs ISR: built on every request / at build time / at build time and revalidated from time to time.
- Server Action vs Route Handler: Server Action for your own forms and mutations; Route Handler for external clients or webhooks.
- Hydration error: server HTML and the first browser render do not match (e.g. Date or a random value during render).
| Mode | When the HTML is built | Example |
|---|---|---|
| SSR | On every request | Per-user dashboard |
| SSG | Once at build time | Docs, marketing page |
| ISR | At build time, then revalidated periodically | Course pages revalidated every hour |
75.7 Machine coding: useDebounce
import { useEffect, useState } from "react"; export function useDebounce(value, ms) { const [debounced, setDebounced] = useState(value); useEffect(() => { const id = setTimeout(() => { setDebounced(value); }, ms); return () => clearTimeout(id); }, [value, ms]); return debounced;}- Machine coding round: build a small feature in 60-90 minutes (debounced search, infinite scroll, todo app).
- The effect sets a timer on every value change; cleanup cancels the old timer.
- While the user is typing, the debounced value does not change; the API call goes out only after typing stops.
- Step 1
1.Clarify: requirements and what is out of scope
- Step 2
2.Working version: simple, end to end first
- Step 3
3.Edge cases: loading, error, empty states
- Step 4
4.Polish: accessibility, cleanup (if time is left)
The whole time: think aloud; the interviewer judges your approach, not only the code
75.8 Common mistakes
- Answering output puzzles in code order instead of sync -> microtasks -> macrotasks.
- Using var in loops with async callbacks; using index as a list key.
- Misconception: memorise a perfect answer for every question. Interviewers check how you break down problems and explain trade-offs; a memorised answer gets caught at the first follow-up.
- Don't know? Say you don't know the exact answer but explain how you would approach it: that is a strong answer too.
75.9 Preparation best practices
- Revise the interview scene of every video in the course: that is your revision list.
- Daily: write one output puzzle or one small component on your own, without AI help.
- Deploy your capstone project and put the link on your resume.
- Do a mock interview with a friend and explain things out loud.
75.10 Interview tips
- Answer any concept question in 3 steps: one line definition, a small example or code, a gotcha or trade-off.
- Example: "The event loop runs callbacks from the queues when the call stack is empty" + the A D F C E B puzzle + "microtasks always run before setTimeout".
- Machine coding order: requirements, working version, edge cases, polish.
- Keep short answers ready for the most asked React and Next.js questions, and know the stale closure fix.