Skip to content

Pro Interview Tips

Maps (or objects) are your go-to when you need to associate keys with values. Use Map when:

ScenarioExample
Frequency counterCount occurrences of each element
Index lookupStore element positions (e.g., Two Sum)
Pair problemsMatch elements that satisfy a condition
Caching / memoizationStore computed results for reuse
GroupingGroup elements by a property
// Frequency counter
const freq = new Map();
for (const item of arr) {
freq.set(item, (freq.get(item) || 0) + 1);
}
// Index lookup
const indexMap = new Map();
arr.forEach((val, i) => indexMap.set(val, i));

Sets are ideal for uniqueness and membership checks. Use Set when:

ScenarioExample
Remove duplicatesDeduplicate an array
Membership checkCheck if an element exists (O(1))
Uniqueness problemsFind unique elements across collections
Cycle detectionTrack visited nodes in graphs
Intersection / unionSet operations between collections
// Remove duplicates
const unique = [...new Set(arr)];
// Membership check
const set = new Set(collection);
if (set.has(target)) { /* O(1) lookup */ }
// Intersection
const intersection = [...new Set(arr1)].filter(x => set2.has(x));

FeatureMapObject
Key typesAny (functions, objects, primitives)Strings & Symbols only
OrderInsertion order guaranteedInteger keys sorted, otherwise insertion order
Sizemap.sizeObject.keys(obj).length
Iterationmap.forEach() or for...offor...in (includes prototype)
PerformanceOptimized for frequent additions/removalsOptimized for static access

Rule of thumb: Use Map unless you need JSON serialization or are working with plain data objects.

// ❌ Slow — O(n²)
function hasDuplicate(arr) {
return arr.some((val, i) => arr.indexOf(val) !== i);
}
// ✅ Fast — O(n)
function hasDuplicate(arr) {
return new Set(arr).size !== arr.length;
}
MethodMutates Original?Returns
push, pop, shift, unshift✅ YesNew length / removed element
splice✅ YesRemoved elements
reverse, sort✅ YesModified array (also returns it)
map, filter, reduce, slice❌ NoNew array / value
concat, includes, find❌ NoNew array / boolean / element

Interview tip: Interviewers often ask about mutability. Be clear about which methods mutate and which don’t.


  1. Clarify — Ask about input size, duplicates, edge cases (empty, single element)
  2. Brute force first — State the obvious O(n²) solution, then optimize
  3. Choose your tool — Map, Set, or both? Frequency counter or complement lookup?
  4. Walk through — Trace through a small example before coding
  5. Test edges — Empty arrays, single elements, all duplicates, no match found

Tip: Master arrays + hash maps + two pointers first — they dominate interviews.

After those, study:

  • Linked Lists
  • Trees
  • Graphs
  • Recursion & Backtracking
  • Sliding Window
  • Dynamic Programming

Related: Array Methods → | Two Pointer → | Study Plan →