Visual DSA Lab
Lesson 01Foundation 12 minutes

Arrays

Store values next to each other and reach any position immediately with an index.

After this lesson
  • Explain why indexed access is constant time
  • Trace insert, delete, and search operations
  • Choose between fixed and dynamic arrays

Mental model

Imagine a numbered row of boxes. Because every box has the same size, the computer can calculate the address of box i without visiting the boxes before it.

Everyday analogy

A row of cinema seats: the seat number takes you directly to the right position.

Learn by doing

Interactive visualization

Try multiple operations. Read the trace after every action and connect the visible movement to the operation's complexity.

Interactive data structure

Change the structure and watch the operation

Try an operation and connect the visible movement to its runtime cost.

0
8
1
3
2
14
3
7
4
11
readyChoose an operation. Array indexes begin at zero.

Reasoning checkpoint

Pause and explain the trace

Try to answer each prompt before revealing the explanation.

01RepresentationHow does the physical representation explain the operations you saw?

Imagine a numbered row of boxes. Because every box has the same size, the computer can calculate the address of box i without visiting the boxes before it.

02CostWhy can “Search” become O(n)?

An unsorted array may require checking every value.

03CorrectnessWhich implementation mistake should you test explicitly?

Reading or writing outside valid indexes

Cost model

Operation complexity

OperationAverageWorstWhy
Access by indexO(1)O(1)The address is calculated directly.
SearchO(n)O(n)An unsorted array may require checking every value.
Insert at endO(1)*O(n)Dynamic arrays occasionally resize and copy.
Insert or delete in middleO(n)O(n)Following values must shift.

* Amortized: occasional expensive work is spread across many cheap operations.

From picture to program

Java implementation

arrays.java
Java
int[] scores = {12, 25, 31, 47};

// Direct access: O(1)
int second = scores[1];

// Linear search: O(n)
for (int i = 0; i < scores.length; i++) {
    if (scores[i] == 31) {
        System.out.println("Found at " + i);
    }
}
Java arrays have a fixed length after creation.
ArrayList adds resizing and convenient insertion methods.
Prefer an array when indexed access and cache locality matter.

Where it appears

  • Ordered collections and lookup tables
  • Matrices, grids, and dynamic-programming tables
  • Building blocks for heaps, hash tables, stacks, and queues

Common mistakes

  • Reading or writing outside valid indexes
  • Forgetting the shift cost of middle insertions
  • Assuming a dynamic array never reallocates
Coding Shuttle Practice•Concept Check

Why can an array read element i in O(1) time?