01 / 14

What is an array in JavaScript?

  1. 1

    An array is a data structure that holds a list of elements.

  2. 2

    Each element is identified by its index, starting from 0. You can create an array using square brackets []

javascript
Difficulty: 2/10
Topics: indexing, mutation, performance

Scenario Questions

0-2 years experience
  1. 1

    You receive an API response with a list of user IDs and need to display the third ID; how would you store and access it using a JavaScript array, and what happens if fewer than three IDs are returned?

  2. 2

    Given let nums = [1, 2, 3];, what does nums[5] evaluate to and why?

  3. 3

    How would you add a new element to the end of an array without mutating the original array?

2-5 years experience
  1. 1

    A function doubles each number in an input array using a for‑loop but sometimes throws undefined is not a function. Walk me through how you'd debug the issue.

  2. 2

    When merging two sorted arrays you notice duplicate entries in the result. Explain a possible cause related to array methods and how you'd fix it.

  3. 3

    Your React component receives a prop that should be an array of objects, but occasionally gets null. How would you safely iterate over it without causing a runtime error?

5-8 years experience
  1. 1

    Our service processes JSON payloads containing arrays up to 10 million items. What memory or performance concerns arise with native arrays, and what alternatives or patterns would you consider?

  2. 2

    We need a sliding window over a large array of events to compute a moving average in real time. Describe an efficient algorithm and discuss the trade‑offs of mutating the original array versus using a circular buffer.

  3. 3

    A legacy codebase frequently uses Array.prototype.splice to remove items, causing UI jank. How would you refactor the code to improve responsiveness while preserving behavior?

8+ years experience
  1. 1

    Our public API returns large collections of data. We're debating between returning a plain JavaScript array versus a paginated cursor/stream. What architectural factors influence your recommendation?

  2. 2

    Multiple teams share a utility library for array manipulation across micro‑frontends. How would you design a versioned, type‑safe helper module to avoid breaking changes?

  3. 3

    We plan to migrate a performance‑critical component from JavaScript arrays to WebAssembly typed arrays. Outline the migration steps and the risks you would mitigate.

Follow-up Questions

  • What is the time complexity of accessing an element by index?
  • How do sparse arrays differ from dense ones in memory usage?
  • When would you prefer `push` over `unshift` for adding items?