An Overview of Arrays and Memory (Data Structures & Algorithms #2)
Channel: CS Dojo
Full video link:
https://www.youtube.com/watch?v=pmN9ExDf3yQData Structure • easy
Arrays are contiguous memory blocks where every element has an index. That direct index access is why arrays are the baseline building block for most other structures.
They are used when you need predictable iteration speed, compact memory layout, and simple serialization across APIs, files, and databases.
Most array interview questions are really about how you traverse: one pass, two passes, two pointers, or sorting first.
Typical Complexity Baseline
| Metric | Value |
|---|---|
| Read by index | O(1) |
| insert/delete in middle | O(n) |
Prefer video learning? This explainer gives a quick visual walkthrough of the core idea before you dive into the detailed sections below.
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
Channel: CS Dojo
Full video link:
https://www.youtube.com/watch?v=pmN9ExDf3yQLearn the core building blocks and terminology in one place before comparisons, so the mechanics are clear and duplicates are removed.
Index
What it is: Zero-based position used for O(1) access.
Why it matters: Index is a required building block for understanding how Array stays correct and performant on large inputs.
Capacity
What it is: Allocated slots; dynamic arrays grow capacity when full.
Why it matters: Capacity is a required building block for understanding how Array stays correct and performant on large inputs.
Length
What it is: Current number of valid items.
Why it matters: Length is a required building block for understanding how Array stays correct and performant on large inputs.
Contiguous memory
What it is: Items are stored in adjacent memory, enabling fast scans.
Why it matters: Contiguous memory is a required building block for understanding how Array stays correct and performant on large inputs.
Resize strategy
What it is: Most dynamic arrays grow geometrically to amortize append cost.
Why it matters: Resize strategy is a required building block for understanding how Array stays correct and performant on large inputs.
Random Access
What it is: Reading element i directly without scanning previous elements.
Why it matters: Reading element i directly without scanning previous elements. In Array, this definition helps you reason about correctness and complexity when inputs scale.
Amortized O(1)
What it is: Average append cost remains constant over many pushes despite occasional resize.
Why it matters: Average append cost remains constant over many pushes despite occasional resize. In Array, this definition helps you reason about correctness and complexity when inputs scale.
In-place
What it is: Algorithm updates the same array rather than allocating a second structure.
Why it matters: Algorithm updates the same array rather than allocating a second structure. In Array, this definition helps you reason about correctness and complexity when inputs scale.
Stable sort
What it is: Equal values keep original order after sorting.
Why it matters: Equal values keep original order after sorting. In Array, this definition helps you reason about correctness and complexity when inputs scale.
Bounds check
What it is: Runtime verification that index is within 0..length-1.
Why it matters: Runtime verification that index is within 0..length-1. In Array, this definition helps you reason about correctness and complexity when inputs scale.
This walkthrough connects the core concepts of Array into one end-to-end execution flow.
Step 1
Zero-based position used for O(1) access.
Before
Array
After
Transition
Why this step matters: Index is a required building block for understanding how Array stays correct and performant on large inputs.
Step 2
Allocated slots; dynamic arrays grow capacity when full.
Before
Array
After
Array
Transition
Why this step matters: Capacity is a required building block for understanding how Array stays correct and performant on large inputs.
Step 3
Current number of valid items.
Before
Array
After
Array
Transition
Why this step matters: Length is a required building block for understanding how Array stays correct and performant on large inputs.
Step 4
Items are stored in adjacent memory, enabling fast scans.
Before
Array
After
Transition
Why this step matters: Contiguous memory is a required building block for understanding how Array stays correct and performant on large inputs.
vs Linked List
When to choose this: Choose arrays when reads by position dominate.
Tradeoff: Middle inserts/deletes are expensive because values shift.
vs Hash Map
When to choose this: Choose arrays when order and numeric indexing matter.
Tradeoff: Lookup by arbitrary key is slower than hash map lookup.
vs Heap / Priority Queue
When to choose this: Choose arrays for full scans and vectorized operations.
Tradeoff: Arrays do not maintain priority ordering automatically.
Meta
Large feed ranking pipelines process candidate posts as arrays/lists before downstream scoring stages.
Takeaway: Array-friendly sequential processing keeps throughput high for ranking workloads.
Snowflake
Columnar analytics engines organize values in array-like vectors for scan-heavy SQL execution.
Takeaway: Contiguous layouts reduce CPU cache misses during huge table scans.
Stripe
Event batching systems often collect records in arrays before validation and persistence.
Takeaway: Simple contiguous batches are easier to serialize and replay safely.
Classic pattern for pair-sum style problems after sorting or when input is already sorted.
Complexity: Time O(n), Space O(1)
Two-pointer pair search in sorted array
function hasPairWithSum(values: number[], target: number): boolean {
let leftIndex = 0
let rightIndex = values.length - 1
while (leftIndex < rightIndex) {
const currentSum = values[leftIndex] + values[rightIndex]
if (currentSum === target) return true
if (currentSum < target) leftIndex += 1
else rightIndex -= 1
}
return false
}
O(n^2), check if one extra map can reduce it to O(n).O(n log n) cost often simplifies everything.Use these signals to decide if this data structure/algorithm is the right fit before implementation.
O(n) or O(n log n) traversal over a linear collection, not graph-style modeling.| # | Problem | Difficulty | Typical Complexity |
|---|---|---|---|
| 1 | Two Sum | Easy | O(n) |
| 2 | Best Time to Buy and Sell Stock | Easy | O(n) |
| 3 | Move Zeroes | Easy | O(n) |
| 4 | Product of Array Except Self | Medium | O(n) |
| 5 | Maximum Subarray | Medium | O(n) |
| 6 | Merge Intervals | Medium | O(n log n) |
| 7 | Set Matrix Zeroes | Medium | O(m*n) |
| 8 | Trapping Rain Water | Hard | O(n) |
| 9 | First Missing Positive | Hard | O(n) |
| 10 | Median of Two Sorted Arrays | Hard | O(log(min(m,n))) |