Download Data Structures & Algorithms (DSA) Handbook | PYQs & Solved Code and more Summaries Data Structures and Algorithms in PDF only on Docsity!
Data Structures
& Algorithms
A Concise Exam & Interview Reference Handbook
Core Concepts • Complexity Analysis • Diagrams • Worked Examples
For B.Tech Computer Science / AI & Data Science Students
Table of Contents
1. Arrays & Strings
2. Linked Lists
3. Stacks & Queues
4. Trees & Binary Search Trees
5. Graphs & Traversals
6. Sorting Algorithms
7. Searching Algorithms
8. Hashing
9. Big-O Complexity Cheat Sheet
10. Common Problem-Solving Patterns
Circular Linked List Loops back to head last node → head
Why use it over an array?
- Insertion/deletion at the head or middle is O(1) once the node is located — no shifting.
- Size grows dynamically without reallocation.
- Trade-off: no O(1) random access, and extra memory for pointers. 3. Stacks & Queues
Stacks and queues are restricted-access linear structures built on arrays or linked lists.
Structure Order Core Ops Typical Uses Stack LIFO push, pop, peek — O(1) Undo, recursion, DFS, expression parsing Queue FIFO enqueue, dequeue — O(1) Scheduling, BFS, buffering
Variants
- Circular Queue:^ wraps around to reuse freed space at the front.
- Deque:^ insertion/removal allowed at both ends.
- Priority Queue:^ elements served by priority, not arrival order — usually backed by a heap, O(log n)
insert/remove.
4. Trees & Binary Search Trees
A tree is a hierarchical structure of nodes with a single root and no cycles. A Binary Search Tree (BST)
additionally guarantees: left subtree < node < right subtree.
Traversal Orders (for the BST above) Traversal Order Rule Result In-order Left → Root → Right 20, 30, 40, 50, 60, 70, 80 Pre-order Root → Left → Right 50, 30, 20, 40, 70, 60, 80 Post-order Left → Right → Root 20, 40, 30, 60, 80, 70, 50
BST Complexity Operation Average Worst (unbalanced) Search / Insert / Delete O(log n) O(n)
Self-balancing variants (AVL, Red-Black Trees) guarantee O(log n) worst case by rebalancing after
insertions/deletions.
Merge Sort O(n log n) O(n log n) O(n log n) O(n) Yes Quick Sort O(n log n) O(n log n) O(n^2) O(log n) No Heap Sort O(n log n) O(n log n) O(n log n) O(1) No
Choosing the right sort
- Merge Sort:^ guaranteed O(n log n), stable — ideal for linked lists and external sorting.
- Quick Sort:^ fastest in practice on average; avoid worst case with randomized pivot.
- Insertion Sort:^ efficient for small or nearly-sorted arrays.
7. Searching Algorithms
Algorithm Precondition Time Complexity Linear Search None O(n) Binary Search Sorted array O(log n) Jump Search Sorted array O(√n) Exponential Search Sorted array O(log n)
Binary Search — Core Logic low, high = 0, n- while low <= high: mid = (low + high) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1
8. Hashing
A hash table maps keys to values using a hash function, giving average O(1) lookup, insert, and delete. Widely
used for frequency counting, caching, and set membership.
Collision Handling
- Chaining:^ each bucket holds a linked list of colliding entries.
- Open Addressing:^ probe for the next free slot (linear, quadratic, or double hashing).
- A good hash function distributes keys uniformly and minimizes clustering. Operation Average Worst Case Insert / Search / Delete O(1) O(n) — many collisions