JavaScript · Interview

JavaScript interview questions

Five JavaScript interview questions about state, intervals, sorting and safe browser storage. Try the output and coding questions yourself before reading the explanation; their printed answers are verified with Node.js at build time and can be run in the browser here.

Every example verified5 questions · 2 concept · 1 predict the output · 1 coding · 1 scenario
ConceptJunior

For totals keyed by item name, when is Map a better choice than a plain object?

Answer

A Map is a good default when keys are dynamic or are not all strings, and when you want an explicit API such as get, set and size. A plain object is fine for simple string-keyed records, especially when serialising to JSON, but create it with Object.create(null) or check own properties if arbitrary user keys could collide with inherited names. Neither structure sorts keys for a report; sort the entries deliberately before printing.

Predict the outputMid-level

What does this code print? Explain why the two shifts are not both active at minute 12.

 JavaScript · what does this print?
const shifts = [[9, 12], [12, 15]];
for (const minute of [11, 12]) {
  console.log(minute, shifts.filter(([start, end]) => start <= minute && minute < end).length);
}

Answer

It prints one active shift at each minute. The first shift includes 9 through the instant before 12, while the second starts at 12. The comparison minute < end makes the interval half-open: [start, end). This convention avoids double-counting a handover and makes adjacent intervals compose neatly. Replacing the final comparison with <= would incorrectly report two people at the boundary.

It prints

11 1
12 1
CodingMid-level

Write a function that counts completed tickets per owner and prints owners alphabetically for the sample data below.

Answer

Walk the ticket rows once, skipping unfinished tickets and incrementing the owner's count in a Map. Convert the entries to an array only for the final alphabetical report. Maya has two completed tickets and Leo has one; the unfinished Maya ticket is ignored. Separating aggregation from sorting makes the function reusable. In a real product, confirm whether unknown owners should be grouped or rejected before presenting totals.

 JavaScript · reference answer
const tickets = [
  { owner: 'maya', done: true }, { owner: 'leo', done: true },
  { owner: 'maya', done: false }, { owner: 'maya', done: true }
];
function completedByOwner(rows) {
  const counts = new Map();
  for (const row of rows) {
    if (row.done) counts.set(row.owner, (counts.get(row.owner) ?? 0) + 1);
  }
  return [...counts].sort(([a], [b]) => a.localeCompare(b));
}
for (const [owner, count] of completedByOwner(tickets)) console.log(owner, count);

Output

leo 1
maya 2
ScenarioSenior

A task board restores saved JSON from localStorage. How would you make startup resilient and keep user-entered titles safe?

Answer

Parse storage in a try/catch, validate that the result is an array of tasks with expected field types, and fall back to a clear empty state if it is corrupt. Save only the fields the app needs rather than serialising DOM nodes or functions. Render a user-entered title with textContent, not innerHTML, so text cannot become markup. Because localStorage is private to that browser and origin, explain that clearing site data removes the board and offer export if the data matters.

ConceptMid-level

In a peak-overlap sweep, why should all starts and ends at the same minute be processed together?

Answer

A shift ending at minute t is no longer active at t, while a shift starting at t is active. If you update the peak after each individual event, an arbitrary sort order among equal-minute events can temporarily count both and invent a peak that never exists. Sum every delta at t first, then compare the resulting active count with the best count. Visit times in increasing order and update only on a strictly larger count to preserve the earliest tied minute.

How this page was checked. Every program on it was run with Node.js 22 at build time; runs in your browser in an isolated Web Worker by the publishing checks, and the output shown is what it printed. The Run buttons execute the same code on your device; nothing you type is sent anywhere.