CMD Guide
HomeDSAArrays

Introduction to Arrays

An array works because it commits to two constraints in exchange for speed: every element has the same fixed byte-size, and all elements sit in one contiguous memory block — so the address of element i is computed by arithmetic (base + i * elementSize) rather than found by traversal.

Recognize the pattern

Brute force vs optimal: the resizing question

The core design trade-off in arrays is not "how do I read an element" (that's always O(1)) — it's "what happens when the array is full and I need one more slot?"

Complexity, derived

Random access: address = base + i·size — one multiply, one add, one dereference → O(1) time, O(1) extra space, regardless of array length.

Doubling amortized cost (accounting argument): consider n appends starting from capacity 1, doubling each time it fills. Resizes happen at sizes 1, 2, 4, 8, ..., up to n. Total copy work = 1+2+4+...+n ≤ 2n (geometric series sums to just under 2n). Spread over n appends, that's O(2n)/n = O(1) amortized per append, even though any single append can spike to O(n).

Insert/delete at index i (non-end): must shift all elements after i by one slot → O(n - i) time, worst case O(n) at index 0, best case O(1) at the end.

Space: a static array of n elements of size s uses exactly n·s bytes. A doubling dynamic array wastes at most ~50% (right after a resize, used = capacity/2+1) — so its space is O(n) with a constant-factor overhead, never asymptotically worse.

Traced example: appends with doubling

OpCapacity beforeActionCapacity afterCopy cost
push(10)0alloc 110
push(20)1full → alloc 2, copy 121
push(30)2full → alloc 4, copy 242
push(40)4fits40
push(50)4full → alloc 8, copy 484

Total copy work for 5 pushes = 0+1+2+0+4 = 7, vs 5 pushes → average ~1.4 per push, bounded by the O(1) amortized guarantee (not literally constant per call, but constant on average).

Java: minimal dynamic array with doubling

class IntArrayList {
    private int[] data = new int[1];
    private int size = 0;

    void push(int val) {
        if (size == data.length) {
            int[] bigger = new int[data.length * 2];
            System.arraycopy(data, 0, bigger, 0, size);
            data = bigger;
        }
        data[size++] = val;
    }

    int get(int i) {
        if (i < 0 || i >= size) throw new IndexOutOfBoundsException();
        return data[i];
    }
}

Pitfalls

When to use / when not — vs. Linked List

Array (dynamic)Linked List
Random accessO(1)O(n)
Insert/delete at frontO(n)O(1)
Insert/delete at endO(1) amortizedO(1) (with tail ptr)
Memory overheadlow, contiguous, cache-friendlyper-node pointer overhead, scattered

Use arrays when you need indexed/random access, iteration speed, or a known/bounded size. Prefer a linked list (or deque) when insertions/deletions at arbitrary positions dominate and random access is rare.

Takeaways

Recall: Why is appending to a doubling dynamic array O(1) amortized even though a single append can cost O(n)?


Synthesized from the source array notes with derivations of amortized doubling cost and array-vs-linked-list trade-offs.

🤖 Don't fully get this? Learn it with Claude

Stuck on Introduction to Arrays? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Introduction to Arrays** (DSA) and want to truly understand it. Explain Introduction to Arrays from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Introduction to Arrays** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Introduction to Arrays** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Introduction to Arrays** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes