Post

DSA Series: Arrays Explained Like You’re a 10x Dev 🧠

A deep dive into how arrays really work — from memory layout and low level behavior to raw implementations and must know DSA patterns.

DSA Series: Arrays Explained Like You’re a 10x Dev 🧠

A deep dive into how arrays really work — from memory layout and low-level behavior to raw implementations and must-know DSA patterns.

📚This blog is part of theData Structures & Algorithms Series, where we break down core DSA topics into in-depth, developer-friendly guides. Be sure to check out the full series to master DSA from the ground up!

🚀 Introduction

Arrays are the bedrock of computer science. Whether you’re manipulating lists of data, building advanced algorithms, or working on systems programming, arrays are everywhere. But have you ever wonderedhow arrays actually work under the hood?

In this deep dive, we’ll explore:

  • How arrays are represented in memory 💾
  • Types of arrays (1D, 2D, multi-type)
  • How arrays are created in C and Python (low-level and high-level)
  • Common operations and performance
  • Real-world interview problems and algorithms
  • Memory layout visualization 📐

Let’s go from justusingarrays to trulyunderstandingthem.


🧱 What is an Array?

Anarrayis a contiguous block of memory used to store multiple elements of the same data type. Each element is accessed by an index, starting from 0.

Key Characteristics:

  • Fixed size
  • Elements stored in contiguous memory locations
  • Constant time accessO(1)
  • Same data type (in low-level languages)

🛠️ Array Implementation (Low-Level)

Arrays may seem simple at the surface in high-level languages like Python or Java, but they are backed by a lot of low-level logic involving memory.

🔧 C Implementation (Raw Memory)

#include <stdio.h>#include <stdlib.h>typedef struct { void* data; size_t size; size_t elementSize;} RawArray;RawArray* createArray(size_t numElements, size_t elementSize) { RawArray* arr = malloc(sizeof(RawArray)); arr->size = numElements; arr->elementSize = elementSize; arr->data = malloc(numElements * elementSize); return arr;}void setInt(RawArray* arr, size_t index, int value) { if (index >= arr->size) { printf("Index out of bounds!\n"); return; } void* target = (char*)arr->data + index * arr->elementSize; *(int*)target = value;}int getInt(RawArray* arr, size_t index) { if (index >= arr->size) { printf("Index out of bounds!\n"); return -1; } void* target = (char*)arr->data + index * arr->elementSize; return *(int*)target;}void destroyArray(RawArray* arr) { free(arr->data); free(arr);}int main() { RawArray* arr = createArray(5, sizeof(int)); setInt(arr, 0, 10); setInt(arr, 1, 20); setInt(arr, 2, 30); setInt(arr, 3, 40); setInt(arr, 4, 50); for (size_t i = 0; i < arr->size; i++) { printf("Element at index %zu: %d\n", i, getInt(arr, i)); } destroyArray(arr); return 0;}/*Output:Element at index 0: 10Element at index 1: 20Element at index 2: 30Element at index 3: 40Element at index 4: 50*/

RawArray — A Peek Under the Hood of Arrays in C

This is a low-level implementation of a dynamic array in C using raw memory (malloc) and pointer arithmetic. It mimics how arrays are managed internally in system-level languages — storing data in a contiguous memory block, calculating element positions using offsets, and manually managing memory.

It’s a great way to understand what happensunder the hoodwhen you use high-level arrays in C, Java, or Python.

Think of this as the raw truth behind arrays — no abstractions, just memory and math.

📌GitHub Repo:Raw Array Implementations (C & Python)


🧠 Array Types

Arrays can be classified based on their usage:

  1. Static Arrays(Fixed size)
  2. Dynamic Arrays(Resizable e.g., Python lists, Java ArrayList)
  3. Multi-dimensional Arrays(Matrix-like data structures)
  4. Jagged Arrays(Array of arrays with different lengths)
  5. Heterogeneous Arrays(Only in certain dynamic languages like Python)

🤔 How Do Multi Data Type Arrays Work?

Languages like Python allow heterogeneous arrays (lists). Internally, each element is a pointer to a PyObject, and the actual type info is resolved at runtime.

For example:

mixed = [1, "hello", 3.14, True]

Each element in the list is a pointer to an object in memory, and the interpreter dynamically checks types during runtime.


🧮 Memory Allocation in Arrays

  • Contiguous block of memory
  • Direct index-based access using offset:address = base + index * size_of_element
  • Fixed size (unless using dynamic/resizable arrays)

Pros:

  • Fast random access
  • Predictable layout

Cons:

  • Resizing is expensive
  • Fixed size (in low-level implementation)

🧠 Memory Layout (Visually)

Let’s break down how arrays are laid out in memory using a simple integer array:

int arr[4] = {10, 20, 30, 40};

Assuming each int takes 4 bytes and the array starts at memory address 0x1000, the layout looks like this:

+------------+------------+------------+------------+| 0x1000 | 0x1004 | 0x1008 | 0x100C || 10 | 20 | 30 | 40 |+------------+------------+------------+------------+ arr[0] arr[1] arr[2] arr[3]

🧩Explanation:

  • Arrays store elements in contiguous memory blocks.
  • Accessing any element isO(1)because we calculate the address like:
  • base_address + (index * size_of_element)
  • This is why arrays arecache-friendlyandfast for lookups.

If you’re showing dynamic arrays (like your RawArray C implementation), the memory layout would look like this:

+-----------+------------+| Meta Info | Data |+-----------+------------+ ↑ ↑ | └──> Contiguous memory block of elements └──> size, elementSize, and data pointer

✅ Data Structures Built on Arrays

  1. Static Arrays
  2. Dynamic Arrays(e.g., ArrayList, vector)
  3. Stacks(array-based)
  4. Queues(including Circular Queues)
  5. Deques(Double-Ended Queues)
  6. Hash Tables(array of buckets or slots)
  7. Heaps(e.g., Min/Max Binary Heap)
  8. Tries(nodes often use arrays for children)
  9. Graphs (Adjacency Matrix)
  10. Matrices(2D arrays)
  11. Strings(as character arrays in many languages)

🔄 Key Algorithms Using Arrays

Searching

  • Linear Search
  • Binary Search

Sorting

  • Bubble, Insertion, Selection Sort
  • QuickSort, MergeSort

Sliding Window Problems

  • Max sum subarray
  • Minimum window substring

Two Pointer Technique

  • Remove duplicates
  • Pair sum

Prefix Sum

  • Subarray sum problems

🧪 Common Interview Problems

  1. Two Sum
  2. Merge Intervals
  3. Maximum Subarray (Kadane’s Algorithm)
  4. Find Duplicate in Array
  5. Trapping Rain Water

🔚 Conclusion

Arrays may appear to be a basic data structure, but understanding their internal working — memory layout, index calculations, and how they translate to machine-level logic — gives you a huge advantage. It’s the first step toward writing optimized and scalable software.


🙋 FAQs

**Q: Can arrays store different data types?
**A: Not in low-level languages like C/C++. Python allows it using object references.

Q: Are arrays faster than linked lists?
A: Yes, for access and cache locality, but slower for frequent insertions/deletions.

Q: How does Python list resize?
A: Python over-allocates memory and resizes when capacity is reached.

🚀 Continue Your DSA Journey

👉 Check out the fullDSA Blog Series hereto master Linked Lists, Trees, and more.

This post is licensed under CC BY 4.0 by the author.