RAPID EXAM REFERENCE · 1-PAGE SUMMARIES

Data Structures Cheat Sheets

One page per topic for when you have 10 minutes before an exam: the core rule, the formulas, and the traps.

Arrays, Stacks, Queues & Linked Lists

Multi-Dimensional Arrays, Address Translation & Stack Mechanics

Open Full Lesson →

In Row-Major 2D array A[L1..U1, L2..U2] with element size c: Addr(A[i, j]) = Base + c * [ (i - L1) * (U2 - L2 + 1) + (j - L2) ].

Must-Remember Principles:
  • —Row-Major 2D: Addr(A[i, j]) = Base + c * [ (i - L1) * N + (j - L2) ] where N = U2 - L2 + 1.
  • —Column-Major 2D: Addr(A[i, j]) = Base + c * [ (j - L2) * M + (i - L1) ] where M = U1 - L1 + 1.
  • —3D Row-Major: Addr(A[i, j, k]) = Base + c * [ (i - L1)*D2*D3 + (j - L2)*D3 + (k - L3) ].
  • —Lower Triangular Matrix (n x n): Stored row-major, element A[i, j] (1-indexed, i >= j) is preceded by i*(i - 1)/2 + (j - 1) elements.
  • —Infix-to-Postfix: Incoming operator popped while in-stack operator has >= precedence (left-associative) or > precedence (right-associative).
  • —Minimum Stacks to implement Queue = 2 Stacks (Amortized O(1) per operation).
Critical Pitfalls & Exam Traps:
  • ×Check array indexing lower bounds (0 vs 1 vs negative index bounds like -3..5).
  • ×Right-associative operators (like exponentiation `^`) have HIGHER incoming precedence than in-stack precedence; identical operators do NOT pop each other.
Arrays, Stacks, Queues & Linked Lists

Queues, Circular Buffers & Linked List Pointer Mechanics

Open Full Lesson →

In Circular Queue of capacity N: rear = (rear + 1) % N. Full condition: (rear + 1) % N == front. Empty condition: front == rear.

Must-Remember Principles:
  • —Circular Queue Size: When one empty slot is reserved, max elements stored = N - 1.
  • —Current Count in Circular Queue: count = (rear - front + N) % N.
  • —Queue using Two Stacks: Enqueue pushes to Stack 1; Dequeue pops from Stack 2 (if empty, pop all from Stack 1 into Stack 2). Amortized O(1).
  • —Floyd's Cycle Detection: Slow pointer moves 1 step, Fast pointer moves 2 steps. If they meet, list has a cycle.
  • —Finding Cycle Start Node: After meeting at distance m, reset Slow to head. Advance both Slow and Fast 1 step at a time; they meet at the cycle start node.
Critical Pitfalls & Exam Traps:
  • ×Linear queue suffers from false overflow: rear reaches N-1 even if front has advanced leaving empty slots at the beginning.
  • ×Reversing a singly linked list in-place requires 3 pointers (prev, curr, next) and O(1) auxiliary space.
Tree Fundamentals, Traversals & Binary Search Trees

Binary Tree Axioms, Degree Theorems & Traversal Reconstruction

Open Full Lesson →

In ANY binary tree, the number of leaf nodes n0 is strictly equal to the number of degree-2 internal nodes plus one: n0 = n2 + 1.

Must-Remember Principles:
  • —Leaf vs Degree-2 Formula: n0 = n2 + 1 (strictly holds for ALL binary trees regardless of balance).
  • —Total Edges: In any tree with n nodes, total edges E = n - 1.
  • —Max Nodes at Level i (root at level 0): 2^i nodes.
  • —Max Nodes in Binary Tree of height h (root at h=0): 2^{h+1} - 1.
  • —Number of Structurally Distinct Binary Trees with n unlabeled nodes: Catalan number C_n = (1 / (n + 1)) * binom(2n, n).
  • —Unique Tree Reconstruction: Requires INORDER + (PREORDER or POSTORDER). Preorder + Postorder is generally AMBIGUOUS.
Critical Pitfalls & Exam Traps:
  • ×Preorder + Postorder CANNOT uniquely reconstruct a general binary tree. It uniquely reconstructs ONLY strictly full binary trees (every node has 0 or 2 children).
  • ×Inorder traversal alone gives ZERO structural information without Preorder or Postorder.
Tree Fundamentals, Traversals & Binary Search Trees

Binary Search Trees: Search, Insertion, Deletion & Successors

Open Full Lesson →

Inorder traversal of ANY valid BST yields strictly monotonically increasing sorted order.

Must-Remember Principles:
  • —BST Invariant: For any node x, all keys in left subtree are < key(x), and all keys in right subtree are > key(x).
  • —Inorder Successor of node x: If x has right subtree => minimum key in right subtree. Else => lowest ancestor of x whose left child is also an ancestor of x.
  • —Deletion Case 1 (Leaf): Simply remove node (set parent's pointer to null).
  • —Deletion Case 2 (One Child): Splice out node by connecting parent directly to child.
  • —Deletion Case 3 (Two Children): Replace node's key with its Inorder Successor (or Predecessor), then delete the successor (which has at most 1 child).
  • —Height Complexity: Best/Average case O(log n); Worst case O(n) (skewed tree on sorted input).
Critical Pitfalls & Exam Traps:
  • ×Predecessor and Successor in a BST with n nodes can be found in O(h) time WITHOUT inspecting all nodes.
  • ×Inserting elements in already-sorted order degrades an ordinary BST into a singly linked list with O(n) search time.
Self-Balancing Trees & Multi-Way Indices

AVL Trees: Height Balancing, Rotations & Minimal Node Bounds

Open Full Lesson →

AVL Balance Factor: BF(node) = Height(LeftSubtree) - Height(RightSubtree) in {-1, 0, +1}. Rebalance immediately at the lowest critical unbalanced node.

Must-Remember Principles:
  • —LL Imbalance: Critical node BF = +2, left child BF = +1 => Single Right Rotation at critical node.
  • —RR Imbalance: Critical node BF = -2, right child BF = -1 => Single Left Rotation at critical node.
  • —LR Imbalance: Critical node BF = +2, left child BF = -1 => Left Rotation at child, then Right Rotation at critical node.
  • —RL Imbalance: Critical node BF = -2, right child BF = +1 => Right Rotation at child, then Left Rotation at critical node.
  • —Minimum Nodes in AVL Tree of height h (root at h=0): N(h) = N(h-1) + N(h-2) + 1, with N(0)=1, N(1)=2.
  • —Maximum Height of AVL tree with n nodes: h <= 1.44 * log2(n + 2) - 0.328 = O(log n).
Critical Pitfalls & Exam Traps:
  • ×When an insertion causes multiple ancestors to become unbalanced, ALWAYS rotate at the LOWEST (deepest) unbalanced node. A single rotation restores balance to the entire tree.
  • ×Be meticulous whether the problem defines height of a single-node tree as 0 or 1.
Self-Balancing Trees & Multi-Way Indices

B-Trees & B+ Trees: Multi-Way Indexing & Block Size Numericals

Open Full Lesson →

In B-Tree of order m: Root has between 2 and m children (1 to m-1 keys). Internal nodes have between ceil(m/2) and m children (ceil(m/2)-1 to m-1 keys).

Must-Remember Principles:
  • —B-Tree Order m Constraints: Max keys per node = m - 1; Min keys for non-root = ceil(m/2) - 1.
  • —B+ Tree Distinctive Property: Internal nodes store keys only as routing guides; all actual record pointers and keys reside in leaf nodes, linked as a doubly linked list.
  • —Internal Node Order Equation (Block B, Key K, Block Pointer P): p * P + (p - 1) * K <= B.
  • —Leaf Node Capacity Equation (Record Pointer Pr): q * (K + Pr) + P <= B.
Critical Pitfalls & Exam Traps:
  • ×In B-Trees, data record pointers are present in ALL nodes (internal and leaf). In B+ Trees, data record pointers are EXCLUSIVELY in leaf nodes.
  • ×Leaf nodes in B+ Trees require 1 block pointer to link to the next sibling leaf for range scanning.
Priority Queues & Binary Heaps

Binary Heaps: Invariants, Linear-Time Build-Heap & Heap Sort

Open Full Lesson →

Build-Heap from an arbitrary array runs in strictly O(n) linear time, NOT O(n log n), because sum_{h=0}^inf (h / 2^h) = 2.

Must-Remember Principles:
  • —Array Indexing (1-indexed): Parent = floor(i/2), Left = 2i, Right = 2i + 1.
  • —Array Indexing (0-indexed): Parent = floor((i - 1)/2), Left = 2i + 1, Right = 2i + 2.
  • —Non-Leaf Internal Nodes: Span indices 1 to floor(n/2). Leaves span floor(n/2) + 1 to n.
  • —Max-Heap Property: A[parent(i)] >= A[i] for all i > 1.
  • —Heap Sort: Build-Heap O(n) + (n-1) Extract-Max calls O(n log n) => Theta(n log n) in-place, unstable.
Critical Pitfalls & Exam Traps:
  • ×Inserting elements one-by-one into an initially empty heap takes O(n log n). Bottom-up Build-Heap takes O(n).
  • ×In a Min-Heap of n elements, the maximum element must be located among the LEAVES (indices floor(n/2) + 1 to n).
Hashing, Collision Dynamics & Disjoint Sets

Hash Tables, Open Addressing & Collision Probing Dynamics

Open Full Lesson →

Double Hashing: h(k, i) = (h1(k) + i * h2(k)) % m eliminates both primary and secondary clustering. h2(k) must be relatively prime to m.

Must-Remember Principles:
  • —Linear Probing: h(k, i) = (h'(k) + i) % m. Suffers from Primary Clustering (contiguous blocks of occupied slots).
  • —Quadratic Probing: h(k, i) = (h'(k) + c1*i + c2*i^2) % m. Eliminates primary clustering; suffers from Secondary Clustering (keys with same initial hash share probe path).
  • —Double Hashing: h(k, i) = (h1(k) + i * h2(k)) % m. Independent hash function h2(k) provides distinct step size for each key.
  • —Load Factor: alpha = n / m (where n is keys stored, m is table size). In open addressing, alpha <= 1 strictly.
Critical Pitfalls & Exam Traps:
  • ×In double hashing, h2(k) must NEVER return 0 (an offset of 0 causes an infinite loop at the initial slot).
  • ×When deleting from an open-addressed hash table, you CANNOT set slot to empty; you must use a 'DELETED' marker (tombstone) to avoid breaking probe chains.
Hashing, Collision Dynamics & Disjoint Sets

Chaining, Universal Hashing & Search Complexity Under Load

Open Full Lesson →

Under Simple Uniform Hashing: Expected time for successful search in chaining is Theta(1 + alpha/2); unsuccessful search is Theta(1 + alpha).

Must-Remember Principles:
  • —Load Factor: alpha = n / m (average keys per slot). In chaining, alpha can exceed 1.
  • —Open Addressing Unsuccessful Search: Expected probes <= 1 / (1 - alpha).
  • —Open Addressing Successful Search: Expected probes <= (1 / alpha) * ln(1 / (1 - alpha)).
  • —Universal Hashing Definition: A family of hash functions H is universal if for any x != y, Pr_{h in H}[h(x) == h(y)] <= 1 / m.
Critical Pitfalls & Exam Traps:
  • ×In open addressing, as load factor alpha approaches 1, the expected number of probes for an unsuccessful search approaches infinity: 1 / (1 - alpha).
Hashing, Collision Dynamics & Disjoint Sets

Disjoint Set Union (DSU): Union by Rank & Path Compression

Open Full Lesson →

DSU with Union by Rank AND Path Compression achieves nearly linear amortized time O(m * alpha(n)) for m operations on n elements.

Must-Remember Principles:
  • —Make-Set(x): Creates single-node tree with parent[x] = x and rank[x] = 0. Time O(1).
  • —Find-Set(x) with Path Compression: Recursively sets parent[x] = Find-Set(parent[x]), flattening tree directly to root.
  • —Union(x, y) by Rank: Attaches root of smaller rank under root of larger rank. Rank increases by 1 ONLY if both roots share equal rank.
  • —Rank Heuristic Upper Bound: A tree with root of rank r has at least 2^r nodes. Hence maximum rank is <= floor(log2 n).
  • —Inverse Ackermann Function: alpha(n) <= 4 for all universe sizes n <= 2^{65536}.
Critical Pitfalls & Exam Traps:
  • ×Union by rank ALONE gives O(m log n). Path compression ALONE gives O(m log n). BOTH together achieve O(m * alpha(n)).
  • ×Path compression does NOT alter the stored rank value of nodes.
GATE DA: Python Programming

Python Fundamentals: Values, Expressions & Control Flow

Open Full Lesson →

Track each variable's current value and type; Python blocks are determined by indentation, and range(stop) excludes stop.

Must-Remember Principles:
  • —`/` performs true division; `//` floors; `%` gives the remainder under Python's division rules.
  • —`range(start, stop, step)` includes start when reached but excludes stop.
  • —`and` and `or` short-circuit and return one of their operands; compare values with `==`, identity with `is`.
  • —A list is mutable; rebinding a name and mutating an object are different operations.
Critical Pitfalls & Exam Traps:
  • ×`bool` is a subtype of `int` in Python (`True == 1`), though they serve different semantic roles.
  • ×Negative `//` floors toward negative infinity: `-7 // 3 == -3`.
  • ×Indentation changes control flow; a loop's `else` runs when the loop completes without `break`.
GATE DA: Python Programming

Python Collections: Lists, Tuples, Dictionaries & Sets

Open Full Lesson →

Use a list for ordered mutable sequences, a tuple for a fixed record, a set for unique membership, and a dictionary for key-to-value lookup.

Must-Remember Principles:
  • —List indexing is O(1); inserting/removing at the front is O(n).
  • —Dictionary and set lookup are expected O(1) with well-behaved hashes; worst-case behavior can be O(n).
  • —A slice `a[i:j]` includes i and excludes j; it creates a shallow copy for a list.
  • —`sorted(xs)` returns a new list; `xs.sort()` mutates the existing list.
Critical Pitfalls & Exam Traps:
  • ×Two names can refer to the same mutable list.
  • ×Dictionary iteration preserves insertion order in modern Python, but do not confuse iteration order with sorted order.
  • ×Set elements and dictionary keys must be hashable; lists are not hashable.
GATE DA: Python Programming

Python Functions, Iteration & Data Transformations

Open Full Lesson →

A function returns a value explicitly; parameters bind to objects, and list comprehensions transform each retained item once.

Must-Remember Principles:
  • —A function call creates a local execution frame; `return` ends that call.
  • —`[f(x) for x in xs if p(x)]` maps then filters while preserving input order.
  • —Use a new output collection when mutation would make aliasing or in-place updates ambiguous.
Critical Pitfalls & Exam Traps:
  • ×A function without an explicit return yields `None`.
  • ×A mutable default argument is created once at function definition, not afresh on each call.
  • ×Comprehension expressions can evaluate filters and transformations in a specified order; trace them in that order.
GATE DA: Python Programming

Python Search & Sorting: Trace the Required Algorithms

Open Full Lesson →

Binary search requires sorted input and shrinks an inclusive interval; insertion sort maintains a sorted prefix.

Must-Remember Principles:
  • —Linear search: O(n) worst case, O(1) extra space.
  • —Binary search: O(log n) comparisons on sorted input.
  • —Selection, bubble, insertion sorts: O(n²) worst case; stable behavior depends on implementation.
  • —Merge sort: O(n log n) time and O(n) auxiliary storage; quicksort has O(n log n) expected and O(n²) worst-case time for common pivot schemes.
Critical Pitfalls & Exam Traps:
  • ×Do not apply binary search to unsorted data.
  • ×In inclusive binary search, update `lo = mid + 1` or `hi = mid - 1` to ensure progress.
  • ×Quicksort complexity depends on pivot choices and input; distinguish expected from worst case.
GATE DA: Python Programming

Implementing Stacks, Queues, Lists, Trees & Hash Tables in Python

Open Full Lesson →

An abstract data type is defined by its operations; a Python built-in may implement it, but its costs and semantics still matter.

Must-Remember Principles:
  • —Stack: list append/pop at end, amortized O(1).
  • —Queue: `collections.deque` append/popleft, O(1) at either end.
  • —Linked-list search is O(n); prepend is O(1) when the head is known.
  • —Dictionary/set key access is expected O(1) under ordinary hash distribution.
Critical Pitfalls & Exam Traps:
  • ×A Python list is a dynamic array, not a linked list.
  • ×A plain Python list used as a queue incurs O(n) front-pop shifting.
  • ×A binary-tree traversal visits each node once: O(n) time.