JavaScript foundations for coding rounds · core
Map and Set in JavaScript
A Map or Set turns 'have I seen this?' and 'how many of these?' into O(1) average lookups, which is the move behind two-sum, grouping, counting and removing duplicates. In JavaScript, use Map rather than a plain object for data keys.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Patterns: complement lookup (two-sum: is target − x already seen?), counting (frequency map), grouping (key → list), dedupe (Set), first unique (count, then scan).
- Map vs object: Map accepts any key type, has size, keeps insertion order and has no prototype keys. A plain object has inherited keys like 'constructor' and 'toString', and 'proto' is special.
- Composite keys: build a string with a separator,
${r},${c}; without a separator, (1, 23) and (12, 3) collide. - Object keys in a Map/Set compare by reference: two different [1, 2] arrays are different keys. Serialise to a string key when you need value equality.
- Map.groupBy and Object.groupBy (ES2024) exist in Node 22+, but writing the loop yourself is expected in interviews.
- Average O(1) per operation; total O(n) time and O(n) extra space. Say the space cost out loud.
The red pen
Common traps: mistake → fix
Mistake: counts[word] = (counts[word] || 0) + 1 works for any word
Fix: for 'constructor' it starts from Object's function and produces a string.
Mistake: A Set of arrays removes duplicate pairs
Fix: arrays compare by reference; use string keys.
Mistake: Two-sum with a map built first can pair an element with itself
Fix: check seen-before while scanning, or check counts.
Easy to hard
Questions interviewers ask, easy to hard
- Two sum: return the indices. Why is a Map better than a nested loop?
- Return every pair, including duplicates. What changes?
- Group anagrams. What key do you use, and what is the complexity?
- Why might a plain object break your counting code?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.