Data Structures & Algorithms
Subject: Data Structures & Algorithms
Comprehensive guide to linear data structures (arrays, linked lists, stacks, queues), non-linear data structures (trees, BST, heaps, graphs), sorting & searching algorithms, and asymptotic complexity analysis.
Concept Summary
Key Revision Rules & Formulas
- ⚡ Asymptotic Growth Order: O(1) < O(log n) < O(n) < O(n log n) < O(n^2) < O(n^3) < O(2^n) < O(n!).
- 🔢 Array Memory Access: Contiguous memory allocation allows O(1) direct access by index calculation: Address(A[i]) = Base + i * Size.
- 🔗 Linked List Operations: Sequential access O(n), but insertion and deletion at a known node position takes O(1) time without shifting elements.
- 🥞 Stack (LIFO): Last-In First-Out structure. Key operations: push(x) O(1), pop() O(1), peek() O(1). Applications: Function recursion stack, expression evaluation (Infix to Postfix).
- 🧍 Queue (FIFO): First-In First-Out structure. Key operations: enqueue(x) O(1), dequeue() O(1). Circular Queue uses modulo math (rear+1)%N to prevent memory wastage.
- 🌳 Binary Search Tree (BST): Left Child < Root <= Right Child. Inorder traversal of a BST ALWAYS yields elements in strictly sorted ascending order.
- 📊 BST Operations: Search, Insert, and Delete take O(h) time, where h is height of tree (O(log n) for balanced BST, O(n) for skewed tree).
- ⛰️ Binary Heap: Complete binary tree represented in array. Heapify unsorted array takes O(n) time. Insertion & Extraction take O(log n) time.
- 🕸️ Graph Representations & Traversals: Adjacency List uses O(V+E) space. BFS uses Queue for level-order exploration. DFS uses Stack/Recursion for deep traversal.
- 🔍 Linear vs Binary Search: Linear Search takes O(n) time on any array. Binary Search takes O(log n) time but REQUIRES a sorted array with random access.
- 🔄 Merge Sort vs Quick Sort: Merge Sort takes guaranteed O(n log n) time and is Stable. Quick Sort takes average O(n log n) but worst-case O(n^2) and is Unstable.
- ⚖️ Sorting Stability: A sorting algorithm is Stable if equal keys preserve their relative input order (Merge Sort, Insertion Sort, Bubble Sort).
Common Exam Pitfalls
- Mistake 1: Confusing Array Index Access with Element Insertion. Array indexing is O(1), but inserting/deleting requires shifting elements taking O(n) time.
- Mistake 2: Forgetting BST Inorder Traversal Property. Inorder traversal of any Binary Search Tree ALWAYS produces a sorted sequence.
- Mistake 3: Memory Wastage in Linear Array Queue. Front element deletion leaves unused space; Circular Queue with (rear+1)%N solves this.
- Mistake 4: Attempting Binary Search on Singly Linked List in O(log n). Singly Linked Lists lack random access, making Binary Search O(n).
- Mistake 5: Assuming Quick Sort is Always O(n log n). Poor pivot selection on already sorted arrays causes Quick Sort to degrade to O(n^2).
- Mistake 6: Thinking Heapifying an Array takes O(n log n). Floyd's Heapify algorithm builds a heap from an array in linear O(n) time.
- Mistake 7: Misunderstanding Stack Overflow vs Underflow. Overflow happens when pushing onto a full stack; Underflow happens when popping from an empty stack.
- Mistake 8: Swapping BFS and DFS Data Structures. BFS requires a Queue (FIFO), while DFS requires a Stack (LIFO or Recursion).
- Mistake 9: Misunderstanding Sorting Stability. Stable sorting preserves original relative order of equal keys (e.g. Merge Sort is stable, Quick Sort is not).
- Mistake 10: Ignoring Auxiliary Call Stack Space in Recursion. Recursive algorithms incur O(h) extra space where h is maximum recursion depth.
Sample Practice Questions
Question 1: टेक्स्ट एन्क्रिप्शन(Text's encryption) टेक्स्ट का _______ है -
- कम्प्रेशन
- एक्सपैंशन
- स्केम्बलिंग ताकि उसकी सुरक्षा बनाए रखी जा सके
- हैशिंग
Explanation: एन्क्रिप्शन में टेक्स्ट को स्क्रैम्बल (अस्पष्ट) करके सुरक्षित किया जाता है ताकि केवल अधिकृत व्यक्ति उसे पढ़ सकें।
Question 2: किसी एलगोरिथम में इनपुट-आउटपुट को दर्शाने के लिए किस ज्यामितीय आकृति का प्रयोग होता है -
- विकर्ण
- वृत्त
- आयत
- समांतर चतुर्भुज
Explanation:
Question 3: किसी भी इनपुट आकार के लिए एल्गोरिथ्म द्वारा लिया जाने वाला अधिकतम समय क्या कहलाता है -
- बेस्ट केस समय जटिलता
- वर्स्ट केस समय जटिलता
- इन्सट्रक्शन स्पेस
- डेटा स्पेस
Explanation:
Question 4: कौन सी डेटा संरचना (data structure) सामने से डेटा तत्वों को हटाने और पीछे की ओर डालने की अनुमति देती है-
- Stacks
- Deques
- Queues
- Binary search tree
Explanation:
Question 5: निम्न में से कौन सी queue का प्रकार नहीं है -
- Simple queue
- Single-ended queue
- Circular queue
- Priority queue
Explanation:
Question 6: लिंक लिस्ट में किस प्रकार का मैमोरी आवंटन होता है -
- स्थिर
- डायनेमिक
- कम्पाइल टाइम
- इनमें से कोई नहीं
Explanation:
Question 8: निम्न prefix एक्सप्रेशन का post fix form क्या है? -M/N*P$QR
- MNPQR$*/-
- M-NPQR$*/
- MNP$QR/-
- M-NQR$*/
Explanation:
Question 9: निम्नलिखित में से कौन सा सॉर्टिंग एल्गोरिदम डिवाइड-एंड-कॉनकॉर (divide-and-conquer) प्रकार है -
- Bubble sort
- Insertion sort
- Quick sort
- All of the above
Explanation:
Question 10: stack में data को जोड़ने के लिए उसे क्या कहते है -
- add
- POP
- push
- इनमे से कोई भी नहीं
Explanation: