Arrays & Strings5 min read

Why Learn Data Structures?

The right container changes everything β€” speed, memory, and elegance
scope: Conceptual

The Right Tool for the Right Job

Imagine you need to find a specific word in a book. You could read every page from the beginning β€” or you could flip to the index at the back and find it in seconds. Both approaches get you the answer. One takes minutes, the other takes seconds.

That's what data structures are about. They're different ways of organizing data so you can access, search, insert, and delete it as efficiently as possible. The data is the same β€” the structure changes everything.

Choosing the wrong data structure is like using a filing cabinet to store a single sticky note, or stuffing a library of books into a shopping bag. It technically works, but it's painfully slow and wastes resources.

Why Should You Care?

Here's the blunt truth: data structures are the single most important topic in computer science interviews. Google, Meta, Amazon, Microsoft β€” every major tech company tests data structures in their coding interviews. But that's not why you should learn them.

You should learn them because they make you a fundamentally better programmer. Once you understand data structures, you'll start seeing problems differently:

  • "I need fast lookups" β†’ use a hash table.
  • "I need sorted data with fast insert" β†’ use a balanced BST or heap.
  • "I need to process things in order" β†’ use a queue.
  • "I need to undo actions" β†’ use a stack.
  • "I need to model relationships" β†’ use a graph.

Without this knowledge, you'll brute-force problems that have elegant, instant solutions. With it, you'll write code that's 10x, 100x, even 1000x faster.

Same Problem, Different Speed

# Task: check if a number exists in a collection of 1 million items
import time
data_list = list(range(1_000_000)) # a list (array)
data_set = set(range(1_000_000)) # a hash set
target = 999_999
# Searching a list: checks every element one by one β†’ O(n)
start = time.time()
found = target in data_list
list_time = time.time() - start
# Searching a set: jumps straight to the answer β†’ O(1)
start = time.time()
found = target in data_set
set_time = time.time() - start
print(f"List search: {list_time:.6f}s")
print(f"Set search: {set_time:.6f}s")
print(f"Set is {list_time / set_time:.0f}x faster!")
Output
List search: 0.014200s
Set search:  0.000001s
Set is 14200x faster!

Same data. Same question. 14,000x faster β€” just by choosing a set instead of a list. That's the power of data structures.

The Core Data Structures

There are dozens of data structures, but they all build on a handful of core ideas. Here's the roadmap:

  • Arrays β€” The simplest structure. A row of boxes, each with a number. Lightning-fast access by index, but resizing is painful.
  • Linked Lists β€” A chain of nodes. Easy to insert and delete, but slow to access by position.
  • Stacks β€” Last in, first out. Think of a stack of plates β€” you always grab the top one.
  • Queues β€” First in, first out. Like a line at a coffee shop.
  • Hash Tables β€” The magic trick. Map keys to values with O(1) lookups. Dictionaries in Python, objects in JavaScript.
  • Trees β€” Hierarchical data. File systems, HTML DOMs, and database indexes all use trees.
  • Heaps β€” Always know the minimum (or maximum). Perfect for priority queues.
  • Graphs β€” Networks of connections. Social networks, maps, and the internet are all graphs.

Complexity

πŸ“¦ Array access by index
Jump straight to the position
Instant O(1)
πŸ” Array search (unsorted)
Check every element one by one
Slow O(n)
πŸ”‘ Hash table lookup
Jump to the key's bucket
Instant O(1)*
🌳 BST search (balanced)
Halve the search space each step
Fast O(log n)
πŸ“‹ Linked list insert (at head)
Just rewire one pointer
Instant O(1)
πŸ“‹ Linked list search
Walk node by node from the head
Slow O(n)
Note: You don't need to memorize every data structure before you start coding. Learn them one at a time, and focus on when to use each one β€” not just how they work. The best programmers don't know the most structures; they know which structure to reach for in any given situation.

Real-World Data Structures

Data structures aren't just academic β€” they're embedded in every app you use:

  • Browser back button β€” A stack of visited pages. Hit back = pop the top.
  • Printer queue β€” A queue. First document in, first document printed.
  • Autocomplete β€” A trie (prefix tree) that narrows matches as you type each letter.
  • Google Maps β€” A graph where intersections are nodes and roads are edges. Dijkstra's algorithm finds the shortest path.
  • Database indexes β€” A B-tree that lets databases find rows in milliseconds among billions.
  • Your contacts list β€” A hash table mapping names to phone numbers.

Quick check

Why was the set 14,000x faster than the list in the code example?

Continue reading