foundation

Arrays and dynamic arrays

Contiguous storage, indexing, resizing, insertion costs, and JavaScript array caveats.

Arrays give indexed access and cache-friendly sequential scans. Dynamic arrays reserve capacity and resize when needed, so append is usually constant time but occasionally copies many elements. JavaScript arrays are higher-level objects, yet interview reasoning still uses array access, scan, insert, and delete costs.

On interviews: Expect tasks around two pointers, prefix sums, in-place mutation, stable order, and when a simple scan beats a more complex structure.

Common pitfalls: Insertion at the front shifts elements. Sparse JavaScript arrays and mutation during iteration can surprise you. Clarify whether the input may be sorted, mutated, or contain duplicates before optimizing.

Checklist:

  • Use indexes deliberately.
  • Mention resize cost for dynamic arrays.
  • Clarify mutation rules.
  • Test empty and one-element inputs.