12 / 19

What is an Array? How is it stored in memory?

Difficulty: 2/10
contiguous memory, indexing, cache locality

Array and Memory Representation

An array is a linear data structure that stores a fixed-size collection of elements of the same data type. The elements are stored in contiguous memory locations, which allows the address of any element to be calculated directly using its index.

javascript
  1. 1

    Array indexing is normally zero-based, so the first element is at index 0.

  2. 2

    Contiguous storage provides excellent cache locality.

  3. 3

    For a one-dimensional array, element access is O(1).

  4. 4

    The array size is fixed in languages such as Java for native arrays; dynamic arrays provide resizable behavior.

Scenario Questions

0-2 years experience

  1. 1We need to store the temperatures for a week. Which data structure would you pick and how would you retrieve Wednesday's value?
  2. 2If you declare an int array of size 5 in C, what does the memory layout look like and how are the elements arranged?
  3. 3What happens if you write to index 5 of a 5‑element array, and why?

2-5 years experience

  1. 1A feature processes a list of user IDs stored in an array but occasionally crashes with out‑of‑bounds errors. How would you debug it and what does the contiguous layout tell you?
  2. 2Why might an array be a poor choice when you need frequent inserts in the middle of a collection, and what alternatives would you consider?
  3. 3When serializing an array to send over the network, how does its contiguous memory affect your serialization approach?

5-8 years experience

  1. 1Our service caches a lookup table of millions of entries in an array. What performance or fragmentation concerns arise at this scale, and how would you mitigate them?
  2. 2Design a component that requires random‑access reads and occasional bulk appends. Explain why you would choose an array versus a linked structure, considering cache locality and garbage‑collector behavior.
  3. 3We need to migrate a legacy C++ module that uses raw arrays to a safer container. What memory‑layout pitfalls must you watch for during the migration?

8+ years experience

  1. 1Multiple services share a binary protocol that packs fixed‑size arrays of sensor readings. How would you evolve the protocol to support variable‑length data without breaking existing clients while preserving contiguous‑memory benefits?
  2. 2At an architectural level, discuss the trade‑offs of using large contiguous arrays for in‑memory analytics pipelines versus chunked storage like memory‑mapped files, focusing on scalability, fault tolerance, and cross‑team maintenance.

Follow-up Questions

  • Can you walk me through how array indexing translates to pointer arithmetic?
  • What impact does cache line size have when you iterate over a large array?
  • How would you handle out‑of‑bounds accesses in a language that doesn't enforce bounds checking?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.