NeetCode
PracticeCoursesRoadmap
Sign InPro
PracticeCoursesRoadmap
Sign InPro
CoursesAlgorithms & Data Structures for Beginners

Progress

4/35

About

  • 0IntroductionFREE1m

Arrays

  • 1RAMFREE6m
  • 2Static Arrays15m
  • 3Dynamic Arrays16m
  • 4Stacks4m

Linked Lists

  • 5Singly Linked ListsFREE12m
  • 6Doubly Linked Lists10m
  • 7Queues4m

Recursion

  • 8Factorial11m
  • 9Fibonacci Sequence13m

Sorting

  • 10Insertion Sort19m
  • 11Merge Sort22m
  • 12Quick Sort17m
  • 13Bucket Sort14m

Binary Search

  • 14Search Array16m
  • 15Search Range8m

Trees

  • 16Binary Tree11m
  • 17Binary Search Tree15m
  • 18BST Insert and Remove22m
  • 19Depth-First Search15m
  • 20Breadth-First Search11m
  • 21BST Sets and Maps6m

Backtracking

  • 22Tree Maze14m

Heap / Priority Queue

  • 23Heap Properties14m
  • 24Push and Pop18m
  • 25Heapify15m

Hashing

  • 26Hash Usage10m
  • 27Hash Implementation29m

Graphs

  • 28Intro to Graphs22m
  • 29Matrix DFS22m
  • 30Matrix BFS14m
  • 31Adjacency List20m

Dynamic Programming

  • 321-Dimension DP20m
  • 332-Dimension DP22m

Bit Manipulation

  • 34Bit Operations17m

Arrays · Chapter 3 · 16 min

Dynamic Arrays

Static arrays are fast but rigid. Dynamic arrays fix the rigidity: they grow as you append to them.

How They Work

A dynamic array wraps a static array plus two numbers: the current length and the allocated capacity. When length reaches capacity, the array allocates a new, larger backing store (usually 2× the current size), copies the elements over, and frees the old one.

# In Python, list is a dynamic array
nums = []
nums.append(1)   # length=1, capacity=4
nums.append(2)   # length=2, capacity=4
nums.append(3)   # length=3, capacity=4
nums.append(4)   # length=4, capacity=4
nums.append(5)   # length=5, capacity=8 (grew!)

Amortized O(1) Append

Most appends are O(1): you just write to the next free slot. Occasionally, an append triggers a resize, which is O(n). But because resizes get rarer as the array grows, the amortized cost is O(1) — averaged over many operations.

Other Operations

Inserting or deleting in the middle still shifts elements, so those remain O(n). Random access by index is still O(1) because the underlying storage is still contiguous.

| Operation | Time | |---|---| | Read / write by index | O(1) | | Append to end | O(1) amortized | | Insert in middle | O(n) | | Delete from middle | O(n) |

Tip: When you know the final size ahead of time, pre-allocate it. This avoids repeated resizing and can meaningfully speed up tight loops.

Previous

Static Arrays

Next

Stacks

On this page