Data Structures &
Algorithms Master Guide
Array ยท Linked List ยท Stack ยท Queue ยท Tree ยท Graph ยท Hashing ยท Sorting ยท Searching ยท Big-O Notation ยท 50 MCQs with Explanations โ for GATE, IBPS, SSC, UPSC, NDA & Campus Placements
FoundationWhat is a Data Structure?
A Data Structure is a method of organising and storing data in a computer so it can be accessed and modified efficiently. Choosing the right data structure directly impacts the speed and memory usage of programs.
| Term | Meaning | Example |
|---|---|---|
| Data | Raw facts (numbers, text, values) | Account number: 12345678 |
| Structure | Organised format for storage | Rows and columns |
| Data Structure | Way to store and manage data efficiently | Array of account numbers |
| Algorithm | Step-by-step solution procedure | Sorting customer names AโZ |
| ADT | Abstract Data Type โ defines behaviour without implementation | Stack, Queue are ADTs |
- Data Structures are classified as Linear (sequential) and Non-Linear (hierarchical/networked)
- Stack and Queue are ADTs (Abstract Data Types) โ keyboards and integers are NOT
- Data structures are stored in RAM (main memory) during execution
- Wrong data structure = slower programs + excess memory usage
- Used in: banking software, OS, databases, networking, AI โ essentially all software
Data Arranged SequentiallyLinear Data Structures
Array
Linear ยท Fixed SizeCollection of same-type elements stored in continuous memory. Access any element instantly using its index. Size fixed at declaration.
Linked List
Linear ยท Dynamic SizeNodes connected via pointers. 10 โ 20 โ 30 โ NULL. Types: Singly, Doubly, Circular. No continuous memory needed.
Stack
LIFO โ Last In, First OutLike a stack of plates โ last placed, first removed. Operations: Push (add) and Pop (remove). Peek to see top without removing.
Queue
FIFO โ First In, First OutLike a bank counter line โ first to arrive, first to be served. Enqueue (insert at rear), Dequeue (remove from front). Variants: Circular, Priority, Deque.
- LIFO โ Stack | FIFO โ Queue โ most tested one-liner
- Stack uses: Undo/Redo, recursion, DFS, expression evaluation, parentheses matching
- Queue uses: BFS, printer scheduling, bank token system, OS scheduling
- Last node of singly linked list has next pointer = NULL
- Circular linked list: last node’s next points to Head
- Doubly linked list: each node has prev + next pointers (traversal both ways)
- Array first index = 0 in most programming languages
Data Arranged Hierarchically / NetworkedNon-Linear Data Structures
Tree
Hierarchical ยท Non-LinearNodes connected in parent-child hierarchy. Root (top), Leaf (no children). Binary Tree: max 2 children per node. BST: left < root < right.
Graph
Network of Nodes & EdgesNodes (vertices) connected by edges. Can be directed/undirected, weighted/unweighted. A Tree is a special case of Graph (connected, acyclic).
Hash Table
Key-Value ยท Fast SearchStores data as key-value pairs. A hash function maps key โ index. Average O(1) for search, insert, delete. Collision = two keys map to same index.
Heap
Complete Binary Tree ยท PriorityMax Heap: largest element at root. Min Heap: smallest at root. Used to implement Priority Queue. Insert + Delete = O(log n).
| Term | Meaning | Exam Trigger |
|---|---|---|
| Root | Topmost node of tree | “Top of tree” = Root |
| Leaf | Node with no children | “No children” = Leaf |
| Height | Longest path from root to leaf | Single node height = 0 (most conventions) |
| BST property | Left < Root < Right | Inorder BST = sorted ascending order |
| Directed Graph | Edges have direction (โ) | Also called Digraph |
| Undirected Graph | Edges have no direction (โ) | Social network friendships |
| Weighted Graph | Edges have values (cost/distance) | Railway route map |
| AVL Tree | Self-balancing BST (balance factor โ1, 0, 1) | Always balanced, O(log n) operations |
| B-Tree / B+ Tree | Multi-way balanced tree | Used in databases and file systems |
Most Tested TableData Structures Comparison
| Feature | Array | Linked List | Stack | Queue |
|---|---|---|---|---|
| Type | Linear | Linear | Linear (restricted) | Linear (restricted) |
| Size | Fixed | Dynamic | Dynamic | Dynamic |
| Access | O(1) by index | O(n) traverse | Only top | Only front/rear |
| Memory | Continuous | Non-continuous | Either | Either |
| Logic | Index | Pointer | LIFO | FIFO |
| Insert middle | O(n) โ shift | O(1) after node | Not applicable | Not applicable |
| Real-life | Book rack | Train coaches | Stack of plates | Bank queue |
Big-O Notation โ Most TestedAlgorithm Complexity
Time Complexity = how long an algorithm takes as input size (n) grows. Space Complexity = how much memory it uses. Big-O notation describes the worst-case upper bound.
| Notation | Name | Meaning | Exam Focus |
|---|---|---|---|
O (Big-O) | Upper bound | Worst case โ maximum time taken | โญโญโญโญโญ Most asked |
ฮ (Theta) | Tight bound | Average / exact growth rate | โญโญโญ |
ฮฉ (Omega) | Lower bound | Best case โ minimum time | โญโญ |
| Complexity | Example Algorithm/Operation | Exam Example |
|---|---|---|
O(1) | Array access by index, Stack push/pop, Hash table access | arr[5] = value |
O(log n) | Binary Search, BST balanced search | Search in sorted 1000 items = ~10 steps |
O(n) | Linear Search, Linked List traversal | Find element in unsorted array |
O(n log n) | Merge Sort, Quick Sort (average), Heap Sort | Best efficient sorting for large data |
O(nยฒ) | Bubble Sort, Selection Sort, Insertion Sort | Basic/naive sorting algorithms |
O(2โฟ) | Recursive Fibonacci, subset enumeration | Exponential โ avoid for large n |
Two Key AlgorithmsSearching Algorithms
| Algorithm | Prerequisite | Best Case | Average Case | Worst Case | Banking Use |
|---|---|---|---|---|---|
| Linear Search | None โ works on unsorted data | O(1) โ first element | O(n/2) | O(n) | Search transaction in unsorted log |
| Binary Search | Data must be sorted | O(1) โ middle element | O(log n) | O(log n) | Search customer by account no. in sorted file |
- Binary Search requires sorted data โ if unsorted, must use linear search
- Binary Search worst case = O(log n) โ much better than O(n) for large datasets
- In 1000 elements: Linear Search = up to 1000 comparisons | Binary Search = up to 10 comparisons
- Binary Search works on arrays (random access) NOT on linked lists (no O(1) access)
- Symbol for array element access:
arr[index]โ uses [ ] brackets
Know Complexities โ Very Frequently TestedSorting Algorithms
| Algorithm | Best Case | Average Case | Worst Case | Space | Stable? | Type |
|---|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(nยฒ) | O(nยฒ) | O(1) | โ Yes | Comparison |
| Selection Sort | O(nยฒ) | O(nยฒ) | O(nยฒ) | O(1) | โ No | Comparison |
| Insertion Sort | O(n) | O(nยฒ) | O(nยฒ) | O(1) | โ Yes | Comparison |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | โ Yes | Divide & Conquer |
| Quick Sort | O(n log n) | O(n log n) | O(nยฒ) | O(log n) | โ No | Divide & Conquer |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | โ No | Heap-based |
- Bubble/Selection/Insertion = O(nยฒ) โ basic, slow for large data
- Merge/Quick/Heap = O(n log n) average โ efficient for large data
- Quick Sort worst case = O(nยฒ) (when pivot is always min/max)
- Merge Sort = Divide and Conquer | Always O(n log n) โ most stable and reliable
- Stable sort = maintains relative order of equal keys (Bubble, Insertion, Merge)
- Heap Sort = uses heap data structure | Space O(1) in-place
All 4 Methods โ Exam FavouriteTree Traversal Methods
- Inorder BST = sorted ascending โ most asked single fact about BST traversal
- Preorder = Root FIRST | Inorder = Root MIDDLE | Postorder = Root LAST
- Level order traversal uses Queue (BFS); DFS uses Stack/recursion
- Preorder โ prefix expression | Inorder โ infix | Postorder โ postfix
- BST with keys [5, 10, 15]: Inorder = 5, 10, 15 (sorted!) โ always verify this
Fast O(1) LookupHashing & Hash Tables
Hashing = technique to map a key to an array index using a hash function, enabling O(1) average search. A Hash Table stores data as key-value pairs. Banking example: customer roll no. โ customer name.
| Term | Meaning | Example |
|---|---|---|
| Hash Function | Maps key โ index in hash table | hash(101) = 101 % 10 = 1 |
| Collision | Two different keys get the same hash index | hash(101) = hash(201) = 1 |
| Chaining | Each index stores a linked list of colliding entries | Index 1 โ [101: Ravi, 201: Priya] |
| Linear Probing | On collision, try next available slot | Index 1 taken โ try index 2, 3โฆ |
| Load Factor | No. of entries / table size โ ideally < 0.7 | Too high = many collisions |
All Operations โ Must MemoriseDS Operations Complexity Summary
| Data Structure | Access | Search | Insert | Delete | Notes |
|---|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) middle | O(n) middle | Fast access, slow middle ops |
| Linked List | O(n) | O(n) | O(1) head | O(1) given node | Fast insert/delete at head |
| Stack | O(n) | O(n) | O(1) push | O(1) pop | LIFO โ only top accessible |
| Queue | O(n) | O(n) | O(1) enqueue | O(1) dequeue | FIFO โ front/rear only |
| BST (balanced) | O(log n) | O(log n) | O(log n) | O(log n) | Skewed BST = O(n) |
| Hash Table | O(1) avg | O(1) avg | O(1) avg | O(1) avg | Worst case O(n) with many collisions |
| Heap (Max/Min) | O(n) | O(n) | O(log n) | O(log n) root | Root access = O(1) |
Real-World & Banking ApplicationsDS & Algorithms in Real Life
Array
Monthly balance for 12 months, IFSC codes list, marks table (2D), interest rate slabs
Linked List
Bank transaction logs (frequent add/remove), browser forward/backward history, adjacency list for graphs
Stack
Undo/Redo in banking software, recursion, DFS traversal, expression evaluation, parentheses matching
Queue
Bank token/counter system, printer job scheduling, BFS, OS round-robin scheduling, call centre
Tree
Bank branch hierarchy (HOโZoneโRegionโBranch), menu structures, BST for sorted account numbers
Graph
ATM/branch network routing, fraud detection patterns, shortest path algorithms, social networks
Hash Table
Customer record indexing, password storage, database indexing, cache systems
Priority Queue/Heap
VIP customer service first, ATM cash denomination selection, CPU job scheduling
B-Tree / B+ Tree
Core banking database indexing on disk, file system organisation (used in Oracle, MySQL)
Circular Queue
Round-robin CPU scheduling, circular token systems, ring buffer for real-time data
Deque
Browser history (back/forward), sliding window algorithms, input-restricted queue systems
Binary Search
Find customer by account number in sorted file, lookup in sorted price tables
| Scenario / Question | Best Data Structure / Algorithm |
|---|---|
| Undo/Redo in bank transaction entry | Stack (two stacks) |
| Bank counter customer token system | Queue |
| Hierarchical branch network (HOโBranch) | Tree |
| ATM/branch route optimisation | Graph + Dijkstra |
| Customer indexing for fast lookup | Hash Table |
| Serving VIP customers first | Priority Queue (Max Heap) |
| Searching sorted account records | Binary Search |
| Database indexing on disk | B-Tree / B+ Tree |
| Fraud pattern detection in accounts | Graph analytics |
| Fixed list of 12 months’ balances | Array |
| BFS in graph / Level order in tree | Queue |
| DFS in graph | Stack / Recursion |
Tap Any Option to Reveal AnswerMCQ Practice โ 50 Questions (5 Chapters)
rear = (rear + 1) % size. Solves the “false overflow” problem of simple linear queues.Last-Minute PrepQuick Revision Flash Cards
๐ฆ Array
- Fixed size, continuous memory
- Access by index = O(1)
- Insert/delete middle = O(n) (shifting)
- First index = 0
- Best for: fixed-size indexed data
๐ Linked List
- Nodes + pointers, dynamic size
- Insert at head = O(1)
- Search/access = O(n)
- Singly: next only | Doubly: prev+next | Circular: last โ head
- Best for: frequent inserts/deletes
๐ Stack
- LIFO = Last In, First Out
- Push/Pop = O(1)
- Overflow: top = maxSizeโ1 | Underflow: top = โ1
- Uses: Undo, recursion, DFS, expression eval
๐ถ Queue
- FIFO = First In, First Out
- Enqueue (rear), Dequeue (front) = O(1)
- Uses: BFS, bank tokens, scheduling
- Circular Queue = Ring Buffer
๐ณ Tree / BST
- Root = top, Leaf = no children
- n nodes โ nโ1 edges
- BST: Left < Root < Right
- Inorder BST = sorted output
- Balanced BST = O(log n) all ops
๐ณ Tree Traversal
- Preorder = Root โ L โ R
- Inorder = L โ Root โ R (sorted for BST)
- Postorder = L โ R โ Root
- Level order = uses Queue (BFS)
๐ Graph
- Vertices + Edges (directed/undirected)
- BFS โ Queue | DFS โ Stack
- Sparse graph โ Adjacency List
- Dense graph โ Adjacency Matrix
- Tree = connected + acyclic graph
๐ Hashing
- Hash function โ key to index
- Search avg = O(1)
- Collision = same index for 2 keys
- Fix: Chaining or Linear Probing
๐ Big-O Order
- O(1) < O(log n) < O(n)
- < O(n log n) < O(nยฒ)
- < O(2โฟ) < O(n!)
- Big-O = worst case
๐ Searching
- Linear Search = O(n) โ any data
- Binary Search = O(log n) โ sorted only
- Binary on 1000 items โ 10 steps
๐ข Sorting
- Bubble/Selection/Insertion = O(nยฒ)
- Merge/Quick avg/Heap = O(n log n)
- Quick Sort worst = O(nยฒ)
- Merge Sort = Divide & Conquer, stable
๐ฆ Banking DS Map
- Queue โ bank token system
- Stack โ Undo/Redo operations
- Tree โ HO โ Branch hierarchy
- Graph โ fraud detection, routing
- B+ Tree โ database indexing
- Hash โ customer lookup
