The Ultimate Data Structures Cheat Sheet
Abstract AlgorithmsTL;DR
What are Data Structures? (The "No-Jargon" Explanation) Imagine you are a carpenter. You have a toolbox. You wouldn't use a hammer to screw in a lightbulb, and you wouldn't use a saw to drive a nail. Data Structures are simply the tools in your codin...

What are Data Structures? (The "No-Jargon" Explanation)
Imagine you are a carpenter. You have a toolbox. You wouldn't use a hammer to screw in a lightbulb, and you wouldn't use a saw to drive a nail.
Data Structures are simply the tools in your coding toolbox.
- Variables are like your pockets—good for holding one or two things.
- Arrays are like a shelf—good for organizing things in a row.
- HashMaps are like a filing cabinet—good for finding things instantly by label.
The "Algorithm" is just the physical action of using the tool. If you pick the wrong tool (Data Structure), the action (Algorithm) will be slow, awkward, or impossible.
1. Linear Data Structures (The Shelf)
These store data sequentially, one after another.
A. Arrays / ArrayLists
- What: Contiguous memory blocks. Like a row of numbered lockers.
- When to use:
- You need to access elements by index instantly ($O(1)$).
- You are iterating sequentially.
- Interview Clues: "Matrix problems", "Sliding window", "Two pointers".
B. Linked Lists
- What: Nodes connected by pointers. Like a treasure hunt where each clue points to the next.
- When to use:
- You need fast insertions/deletions at the start/end ($O(1)$).
- You don't know how much data you'll have (dynamic size).
- Interview Clues: "Reverse a list", "Detect a cycle", "Merge two sorted lists".
C. Stacks
- What: LIFO (Last In, First Out). Like a stack of pancakes. You eat the top one first.
- When to use:
- Reversing things (words, navigation history).
- Processing nested structures (parentheses, code execution).
- Interview Clues: "Valid Parentheses", "Back button", "Depth First Search (DFS)".
D. Queues
- What: FIFO (First In, First Out). Like a line at a coffee shop.
- When to use:
- Processing tasks in the order they arrived.
- Breadth First Search (BFS).
- Interview Clues: "Level order traversal", "Shortest path in unweighted graph".
2. Hashing Data Structures (The Filing Cabinet)
A. HashMaps (Hash Tables)
- What: Key-Value pairs.
- When to use:
- The Golden Rule: If you need to find something in $O(1)$ time, use a HashMap.
- Counting frequencies (e.g., counting votes).
- Interview Clues: "Two Sum", "Group Anagrams", "First unique character".
Deep Dive: Why the Choice Matters (Array vs. HashMap)
Let's look at a concrete example to see why picking the right structure changes everything.
Toy Dataset: A Fruit Shop Inventory We have 5 items. We want to find the price of "Elderberry".
| Index | Product | Price |
| 0 | Apple | $1 |
| 1 | Banana | $2 |
| 2 | Cherry | $3 |
| 3 | Date | $4 |
| 4 | Elderberry | $5 |
Scenario A: Using an Array (The "Wrong" Way) If we store this as a list of pairs, the computer doesn't know where "Elderberry" is.
- Check Index 0: "Apple"? No.
- Check Index 1: "Banana"? No.
- Check Index 2: "Cherry"? No.
- Check Index 3: "Date"? No.
- Check Index 4: "Elderberry"? Yes. Return $5.
Result: We had to check 5 items. If we had 1 million items, we'd check 1 million. Math: Linear Time — $O(N)$.
Scenario B: Using a HashMap (The "Right" Way) A HashMap uses a "Hash Function" to calculate the address directly.
- Input: "Elderberry"
- Hash Function:
hash("Elderberry")$\rightarrow$ returns index4. - Lookup: Go directly to memory slot
4. Return $5.
Result: We checked 1 item. If we had 1 million items, we'd still check 1 item. Math: Constant Time — $O(1)$.
The Mathematical Goal We want to minimize the Time Complexity: $$ T(n) = O(1) \ll O(n) $$ Constant time is always faster than Linear time for large datasets.
3. Tree & Graph Data Structures (The Network)
A. Binary Trees & BST
- What: Hierarchical data. Each node has max 2 children.
- When to use:
- Sorted data that needs frequent updates (BST).
- Hierarchies (File systems, Org charts).
- Interview Clues: "Find height", "Lowest Common Ancestor (LCA)", "Kth smallest".
B. Heaps (Priority Queues)
- What: A tree where the root is always the "VIP" (Min or Max).
- When to use:
- You don't care about the whole list, you only care about the Top K or the Best/Worst right now.
- Interview Clues: "Kth largest element", "Merge K sorted lists".
C. Graphs
- What: Nodes connected by edges.
- When to use:
- Connections, Social Networks, Maps.
- Interview Clues: "Shortest path", "Course Schedule", "Number of Islands".
D. Tries (Prefix Trees)
- What: A tree optimized for text.
- When to use:
- Searching for words starting with a prefix.
- Interview Clues: "Autocomplete", "Word Search", "Spell Checker".
Real-World Application: Google Search Autocomplete
- Input: User types "Alg".
- The Problem: We have a dictionary of 1,000,000 words. We need to instantly show "Algorithm", "Algebra", "Algiers".
- Bad Choice (Array): Scan all 1 million words. Check if they start with "Alg". Too slow.
- Good Choice (Trie):
- Process: The data is stored in a tree. Root -> 'A' -> 'l' -> 'g'.
- Traversal: The computer follows the path A-L-G.
- Output: It instantly sees the branches hanging off 'G': "orithm", "ebra", "iers".
- Usage: This allows Google to suggest words while you type, with zero lag.
Summary Table: The "Cheat Sheet"
| Problem Requirement | Recommended Data Structure |
| Fast Lookup / Duplicates | HashMap / HashSet |
| Top K Elements / Min-Max | Heap (Priority Queue) |
| Recursion / Backtracking | Stack |
| Shortest Path (Unweighted) | Queue (BFS) |
| Shortest Path (Weighted) | Min-Heap (Dijkstra) |
| Prefix Search | Trie |
| Sorted Data + Updates | BST (TreeMap) |
| Connectivity / Cycles | Union-Find / DFS |
Practice Quiz: Test Your Intuition
Scenario: You are building a "Undo" button for a text editor. Which data structure tracks the changes?
- A) Queue
- B) Stack
- C) HashMap
Scenario: You need to find the 10 most popular tweets out of a stream of 1 billion tweets.
- A) Array
- B) Binary Search Tree
- C) Heap (Priority Queue)
(Answers: 1-B, 2-C)
What's Next?
Now that you have the map, it's time to start walking. In the next post, we will dive deep into Linked Lists—the simplest dynamic data structure—and learn how to manipulate pointers like a pro.
Tags

Written by
Abstract Algorithms
@abstractalgorithms
