Skip to content
Skilled.dev

Arrays | Coding Interview Data Structure

Understand array indexing, insertion, iteration, and resizing, then practice the invariants used in array and matrix interview problems.

11:11 · Trey Huffine · Published 2025-10-15

Watch on YouTube · Read lesson notes · Open the current lesson

Chapters

  1. 0:00 Introduction
  2. 0:25 Arrays Overview
  3. 1:51 Fast Operations (push, pop, index access)
  4. 3:23 Operations That Move or Search Elements
  5. 4:53 Big O
  6. 5:55 Pros and Cons
  7. 7:23 Static vs. Dynamic Arrays
  8. 9:49 Amortized Time (advanced)

Selected start: 0:00. Press play in the video to watch.

Changes since recording

  • A Java `int` takes 4 bytes on any operating system, so `new int[20]` needs 80 bytes.

Use the current lesson code when checking behavior.

Lesson notes

These notes explain the problem and current implementation. They are a reading companion, not a verbatim transcript of the recording.

An array puts an ordered sequence behind numeric indexes. The useful question in an interview is which operations can reach an element directly and which must move other elements. The recording introduces that distinction, then uses capacity and resizing to explain why a dynamic array can grow without making every append expensive.

Trace an insertion before naming its cost

Start with [10, 20, 30, 40]. Reading index 2 gives 30 without scanning the preceding values. Inserting 15 at index 1 changes the positions of 20, 30, and 40. Those shifts explain the linear work. Removing index 1 closes the same gap in the other direction. Finding the value 30 is a separate operation from reading a known index: an unsorted array may need a scan to find it.

Appending leaves existing indexes alone when spare capacity is available. A dynamic array occasionally allocates more storage and copies its elements. With geometric capacity growth, those copies add up to linear work over a long sequence of appends, giving amortized constant time per append. A single append that triggers a resize can still take linear time.

Keep the representation assumptions visible

The contiguous, fixed-width array model gives a useful explanation for direct indexed access. A JavaScript array has additional language behavior, including holes and object properties, so that model does not describe every JavaScript array implementation detail. Use the model to reason about the exercise, and distinguish it from promises made by a particular runtime.

The correction above also separates an element's size from the complete allocation. Twenty Java integers require 80 bytes for their values; an actual array object has additional overhead. Changing the operating system does not change the Java integer's specified width.

Practice with operations that look similar

Compare inserting at the beginning, middle, and end. State whether your estimate is worst-case, amortized, or conditional on spare capacity. Include the returned array separately when discussing extra storage. Try an empty array and a full-capacity example before claiming an operation always takes the same work.

Continue with Implement an Array, where shifting and length updates become explicit code, or use the array topic path to move into matrix traversal and product problems.

More in Arrays