Interview Q&A · JavaScript · 14 questions in this topic

Common Coding Tasks — JavaScript Interview Q&A

All JavaScript topics

Implement-it-now favorites — flattening, grouping, debounce, deep equality and promise utilities. Part of Tricky Outputs & Coding Tasks · 204 questions in the JavaScript bank.

How do you reverse a string (Unicode-safe)?

Answer

Spread by code points ([...s]) then reverse-join — length-based indexing breaks emoji (surrogate pairs). Grapheme clusters (family emoji) need Intl.Segmenter for full correctness.

Code-points-not-units is the correctness rule.

Example

Example.jsjs
const rev = (s) => [...s].reverse().join('');
rev('hello'); // 'olleh'

How do you check anagrams?

Answer

Normalize (case), frequency-count one string, verify with the other — O(n). Sorting both (O(n log n)) is the simpler alternative. Early-exit on length mismatch.

Count-then-verify is the linear pattern.

Example

Example.jsjs
function anagram(a, b) {
  if (a.length !== b.length) return false;
  const c = {};
  for (const ch of a.toLowerCase()) c[ch] = (c[ch] || 0) + 1;
  for (const ch of b.toLowerCase()) if (!c[ch]--) return false;
  return true;
}

How do you dedupe an array (and by key)?

Answer

Primitives: [...new Set(arr)]. Objects by key: Map keyed on the key field, keep first. SameValueZero makes NaN dedupe correctly under Set.

Set-for-values, Map-by-key-for-entities.

Example

Example.jsjs
const uniq = [...new Set([1, 1, NaN, NaN])]; // [1, NaN]
// by key: new Map(users.map((u) => [u.id, u]))

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits