Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
JavaScript for Frontend Interviews
  1. 1.Introduction to JavaScript & Setup
  2. 2.Variables and Data Types
  3. 3.Operators and Expressions
  4. 4.Control Flow: Conditionals
  5. 5.Control Flow: Loops
  6. 6.Functions: Basics
  7. 7.Arrays and Objects
  8. 8.Higher-Order Functions & Callbacks
  9. 9.Scope and Closures
  10. 10.`this` Keyword and Context
  11. 11.Asynchronous JavaScript: Callbacks & Promises
  12. 12.ES6+ Features (Part 1)
  13. 13.ES6+ Features (Part 2)
  14. 14.Error Handling & Debugging
  15. 15.Asynchronous JavaScript: Async/Await
  16. 16.Prototypal Inheritance & `new` Keyword
  17. 17.Event Loop, Microtasks & Macrotasks
  18. 18.Working with the DOM
  19. 19.Advanced Array Methods & Data Structures
  20. 20.Frontend Interview Prep & Best Practices
JavaScript for Frontend Interviews
19

JavaScript · Lesson 19 of 20 · 4 min

Advanced Array Methods & Data Structures

This is about showing you can choose the right tool for the job, which signals an understanding of performance trade-offs.

Answering a coding challenge with a nested for loop is a junior move. Using the right data structure, like a Set or Map, to get a linear-time solution is how you show you're a senior. This lesson covers the tools that get you there.

reduce: The Double-Edged Sword

reduce is the most powerful array method, but it's often overused for "clever" one-liners that are hard to read. Use it for its intended purpose: deriving a single value from a list in one pass. That single value can be a number (summing), a new array (flattening), or, most usefully, an object (grouping).

The trap: Forgetting the initialValue. If you reduce an empty array without an initial value, it throws a TypeError. If you're building an object or a new array, you must provide an empty object ({}) or array ([]) as the second argument. Always provide it.

In an interview, you might be asked to implement map or filter using reduce. It’s a good exercise to test your understanding of the reducer pattern. In production code, just use map and filter. They are more readable and state their intent clearly.

Use Set for Uniqueness and Existence Checks

If you need to ask "is this item in the collection?" many times, an array is the wrong choice. Every array.includes(item) call is an O(N) operation, meaning it may have to scan the entire array.

A Set gives you an O(1) average time complexity for has(). For a large collection, the difference is enormous. Pre-building a Set from an array costs O(N), but every subsequent check is nearly instantaneous.

This also makes Set the best way to de-duplicate an array.

const numbers = [1, 5, 2, 6, 2, 5, 1];
const uniqueNumbers = [...new Set(numbers)]; // [1, 5, 2, 6]

The trap: A Set stores unique object references, not deep values. new Set([{ a: 1 }, { a: 1 }]) will contain two separate objects because they have different references in memory.

Use Map When Object Keys Aren't Enough

A plain JavaScript Object is a hash map, but it has a major limitation: keys are always coerced into strings. You can't use an object or a function as a key.

A Map allows keys of any type. This makes it the correct tool for associating metadata with objects without modifying them. Think of mapping a DOM node to its React component instance, or caching the result of a function. Map also preserves insertion order, which can be useful.

For classic interview problems like frequency counting ("find the most common character in a string"), a Map is the idiomatic solution. It's slightly more performant for frequent additions and removals than a plain object.

The reduce Grouping Pattern

One of the most practical and common uses for reduce is to group an array of objects into a new object, keyed by some property. This is a pattern you should know by heart.

Here, we take a flat list of products and transform it into an object where keys are categories and values are arrays of products in that category.

const products = [
  { id: 1, name: "Laptop", category: "Electronics" },
  { id: 2, name: "Keyboard", category: "Electronics" },
  { id: 4, name: "T-Shirt", category: "Apparel" },
  { id: 5, name: "Jeans", category: "Apparel" },
];

const productsByCategory = products.reduce((acc, product) => {
  const { category } = product;
  if (!acc[category]) {
    acc[category] = [];
  }
  acc[category].push(product);
  return acc;
}, {});

/*
productsByCategory is now:
{
  Electronics: [
    { id: 1, name: 'Laptop', category: 'Electronics' },
    { id: 2, name: 'Keyboard', category: 'Electronics' }
  ],
  Apparel: [
    { id: 4, name: 'T-Shirt', category: 'Apparel' },
    { id: 5, name: 'Jeans', category: 'Apparel' }
  ]
}
*/

This single pass is more efficient than iterating multiple times to find all items for each category.

WeakMap for Memory-Safe Metadata

A WeakMap is a Map where the keys are held "weakly." If an object used as a key in a WeakMap has no other references to it in your program, the garbage collector can remove it, and the corresponding entry in the WeakMap will also be removed automatically.

This is the solution for preventing memory leaks.

The classic scenario is attaching metadata to a DOM element. If you use a regular Map and the DOM element is later removed, your Map still holds a reference to it, preventing it from being garbage collected. A WeakMap solves this.

The trade-off is that WeakMaps are not iterable and have no size property. You can only set, get, has, and delete specific entries.

Try it

Write a function that takes an array of numbers and returns the most frequent number. Use a Map to store the frequency of each number. This should take you about 15 minutes. You can find a similar problem on the site at /problems/majority-element.

Next: We'll cover how to structure your thinking and communication during the interview in "Frontend Interview Prep & Best Practices".

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Previous

Working with the DOM

Next

Frontend Interview Prep & Best Practices