← dsa-notebook
Interview Prep — Cheat Sheet

Data Structures

12 structures create · insert · delete · traverse
Linear · Indexed · Python built-in

Array / List

Dynamic array backed by contiguous memory. O(1) random access and amortized O(1) append. The default Python container — use it as a list, stack, or combine with slicing for prefix sums.

create
step 0 / 7
Python Reference
Complexity
Key-Value · Hash Table · dict

Hash Map

Unordered key→value store backed by a hash table. O(1) average get/set/delete and membership check. Python's dict preserves insertion order since 3.7. Use defaultdict and Counter for frequency counting.

create
step 0 / 6
Python Reference
Complexity
Unique Elements · Hash Table · set

Hash Set

Unordered collection of unique elements. O(1) average add/remove/membership. Use when you need fast deduplication or O(1) "have I seen this?" checks — the set's superpower over lists.

create
step 0 / 5
Python Reference
Complexity
LIFO · Linear · list

Stack

Last-In-First-Out. Push and pop from the same end (the top). Python's list works perfectly — append() to push, pop() to pop. Monotonic stacks are the key interview pattern.

create
step 0 / 6
Python Reference
Complexity
FIFO · Linear · deque

Queue

First-In-First-Out. Enqueue at the back, dequeue from the front. Always use collections.dequelist.pop(0) is O(n). The BFS traversal pattern lives here.

create
step 0 / 5
Python Reference
Complexity
Double-Ended · Linear · deque

Deque

O(1) append and pop at both ends. Generalizes stack and queue. Key pattern: monotonic deque for sliding window maximum. Also use maxlen for a bounded window that auto-evicts old elements.

create
step 0 / 6
Python Reference
Complexity
Pointer · Linear · ListNode

Linked List

Nodes linked by next pointers. O(1) head insert/delete, O(n) search. The dummy head pattern eliminates edge cases. Two-pointer tricks (fast/slow) detect cycles and find midpoints.

define
step 0 / 6
Python Reference
Complexity
Tree · Recursive · TreeNode

Binary Tree / BST

Each node has at most two children. BST property: left subtree < node < right subtree. In-order traversal yields a sorted sequence. O(log n) operations on a balanced tree, O(n) worst case (degenerate).

create
step 0 / 7
Python Reference
Complexity
Priority Queue · Tree · heapq

Heap (min-heap)

Complete binary tree where every parent ≤ its children. heap[0] is always the minimum. Python's heapq is a min-heap. For max-heap: negate values. O(log n) push/pop, O(1) peek, O(n) heapify.

create
step 0 / 6
Python Reference
Complexity
Adjacency List · BFS / DFS · defaultdict

Graph

Nodes (vertices) connected by edges. Represent as an adjacency list with defaultdict(list). BFS finds shortest paths (unweighted). DFS explores depth-first and powers cycle detection, topological sort, and connected components.

create
step 0 / 6
Python Reference
Complexity
Prefix Tree · String · TrieNode

Trie

Tree for storing strings character by character. Common prefixes share nodes. O(m) insert/search/startsWith where m = word length. Best structure for prefix matching, autocomplete, and word existence checks.

create
step 0 / 6
Python Reference
Complexity
Disjoint Sets · Path Compression · Rank

Union -Find

Tracks which elements belong to the same connected component. Near-O(1) per operation with path compression and union by rank. Essential for Kruskal's MST, cycle detection in undirected graphs, and grid connectivity problems.

make_set
step 0 / 6
Python Reference
Complexity