|
| 1 | +--- |
| 2 | +id: 699c2dd0e2e1f947791f33fc |
| 3 | +title: Data Structures Review |
| 4 | +challengeType: 31 |
| 5 | +dashedName: review-data-structures-js |
| 6 | +--- |
| 7 | + |
| 8 | +# --interactive-- |
| 9 | + |
| 10 | +## Algorithms and Big O Notation |
| 11 | + |
| 12 | +- **Algorithms**: A set of unambiguous instructions for solving a problem or carrying out a task. Algorithms must finish in a finite number of steps and each step must be precise and unambiguous. |
| 13 | + |
| 14 | +- **Big O Notation**: Describes the worst-case performance, or growth rate, of an algorithm as the input size increases. It focuses on how resource usage grows with input size, ignoring constant factors and lower-order terms. |
| 15 | + |
| 16 | +### Common Time Complexities |
| 17 | + |
| 18 | +- **O(1) - Constant Time**: Algorithm takes the same amount of time regardless of input size. |
| 19 | + |
| 20 | +:::interactive_editor |
| 21 | + |
| 22 | +```js |
| 23 | +function checkEvenOrOdd(number) { |
| 24 | + if (number % 2 === 0) { |
| 25 | + return "Even"; |
| 26 | + } else { |
| 27 | + return "Odd"; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +console.log(checkEvenOrOdd(4)) // "Even" |
| 32 | +console.log(checkEvenOrOdd(5)) // "Odd" |
| 33 | +``` |
| 34 | + |
| 35 | +::: |
| 36 | + |
| 37 | +- **O(log n) - Logarithmic Time**: Time increases slowly as input grows. Common in algorithms that repeatedly reduce problem size by a fraction (like Binary Search). |
| 38 | + |
| 39 | +- **O(n) - Linear Time**: Running time increases proportionally to input size. |
| 40 | + |
| 41 | +```js |
| 42 | +for (const grade of grades) { // grades is an array |
| 43 | + console.log(grade); |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +- **O(n log n) - Log-Linear Time**: Common time complexity of efficient sorting algorithms like Merge Sort and Quick Sort. |
| 48 | + |
| 49 | +- **O(n²) - Quadratic Time**: Running time increases quadratically. Often seen in nested loops. |
| 50 | + |
| 51 | +:::interactive_editor |
| 52 | + |
| 53 | +```js |
| 54 | +const n = 3; |
| 55 | + |
| 56 | +for (let i = 0; i < n; i++) { |
| 57 | + for (let j = 0; j < n; j++) { |
| 58 | + console.log("Hello, World!"); |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +::: |
| 64 | + |
| 65 | +### Space Complexity |
| 66 | + |
| 67 | +- **O(1) - Constant Space**: Algorithm uses same amount of memory regardless of input size. |
| 68 | +- **O(n) - Linear Space**: Memory usage grows proportionally with input size. |
| 69 | +- **O(n²) - Quadratic Space**: Memory usage grows quadratically with input size. |
| 70 | + |
| 71 | +## Problem-Solving Techniques |
| 72 | + |
| 73 | +- **Understanding the Problem**: Read the problem statement multiple times. Identify the input, expected output, and how to transform input to output. |
| 74 | + |
| 75 | +- **Pseudocode**: High-level description of algorithm logic that is language-independent. Uses common written language mixed with programming constructs like `IF`, `ELSE`, `FOR`, `WHILE`. |
| 76 | + |
| 77 | +```md |
| 78 | +GET original_string |
| 79 | +SET reversed_string = "" |
| 80 | +FOR EACH character IN original_string: |
| 81 | + ADD character TO THE BEGINNING OF reversed_string |
| 82 | +DISPLAY reversed_string |
| 83 | +``` |
| 84 | + |
| 85 | +- **Edge Cases**: Specific, valid inputs that occur at the boundaries of what an algorithm should handle. Always consider and test edge cases. |
| 86 | + |
| 87 | +## Arrays |
| 88 | + |
| 89 | +- **Static Arrays**: Have a fixed size determined at initialization. Elements stored in adjacent memory locations. Size cannot be changed during program execution. |
| 90 | + |
| 91 | +- **Dynamic Arrays**: Can grow or shrink automatically during program execution. Handle resizing through automatic copying to larger arrays when needed. |
| 92 | + |
| 93 | +### Dynamic Arrays Implementation in JavaScript |
| 94 | + |
| 95 | +:::interactive_editor |
| 96 | + |
| 97 | +```js |
| 98 | +let numbers = [3, 4, 5, 6]; |
| 99 | +// Access elements |
| 100 | +console.log(numbers[0]) // 3 |
| 101 | + |
| 102 | +// Update elements |
| 103 | +numbers[2] = 16 |
| 104 | +console.log([...numbers]); // [3, 4, 16, 6] |
| 105 | + |
| 106 | +// Add elements |
| 107 | +numbers.push(7) |
| 108 | +console.log([...numbers]); // [3, 4, 16, 6, 7] |
| 109 | + |
| 110 | +// Add at a specific index |
| 111 | +numbers.splice(3, 0, 15); |
| 112 | +console.log([...numbers]); // [3, 4, 16, 15, 6, 7] |
| 113 | + |
| 114 | +// Remove elements |
| 115 | +numbers.splice(2, 1); // Remove at specific index |
| 116 | +console.log([...numbers]); // [3, 4, 15, 6, 7] |
| 117 | + |
| 118 | +numbers.pop() // Remove last element |
| 119 | +console.log([...numbers]); // [3, 4, 15, 6] |
| 120 | +``` |
| 121 | + |
| 122 | +::: |
| 123 | + |
| 124 | +### Time Complexities for Dynamic Arrays |
| 125 | + |
| 126 | +- **Access**: O(1) |
| 127 | +- **Insert at end**: O(1) average, O(n) when resizing needed |
| 128 | +- **Insert in middle**: O(n) |
| 129 | +- **Delete**: O(n) for middle, O(1) for end |
| 130 | + |
| 131 | +## Stacks |
| 132 | + |
| 133 | +- **Stacks**: Last-In, First-Out (LIFO) data structure. Elements added and removed from the top only. |
| 134 | + |
| 135 | +- **Push Operation**: Adding an element to the top of the stack. Time complexity: O(1). |
| 136 | + |
| 137 | +- **Pop Operation**: Removing an element from the top of the stack. Time complexity: O(1). |
| 138 | + |
| 139 | +:::interactive_editor |
| 140 | + |
| 141 | +```js |
| 142 | +// Using JavaScript array as a stack |
| 143 | + |
| 144 | +let stack = []; |
| 145 | + |
| 146 | +// Push operations |
| 147 | +stack.push(1); |
| 148 | +stack.push(2); |
| 149 | +stack.push(3); |
| 150 | +console.log([...stack]); // [1, 2, 3] |
| 151 | + |
| 152 | +// Pop operations |
| 153 | +let topElement = stack.pop(); |
| 154 | +console.log(topElement); // 3 |
| 155 | +console.log([...stack]); // [1, 2] |
| 156 | +``` |
| 157 | + |
| 158 | +::: |
| 159 | + |
| 160 | +## Queues |
| 161 | + |
| 162 | +- **Queues**: First-In, First-Out (FIFO) data structure. Elements added to the back and removed from the front. |
| 163 | + |
| 164 | +- **Enqueue Operation**: Adding an element to the back of the queue. Time complexity: O(1). |
| 165 | + |
| 166 | +- **Dequeue Operation**: Removing an element from the front of the queue. Time complexity: O(1). |
| 167 | + |
| 168 | +:::interactive_editor |
| 169 | + |
| 170 | +```js |
| 171 | +// Using JavaScript array as a queue |
| 172 | +let queue = []; |
| 173 | + |
| 174 | +// Enqueue operations |
| 175 | +queue.push(1); |
| 176 | +queue.push(2); |
| 177 | +queue.push(3); |
| 178 | +console.log([...queue]); // [1, 2, 3] |
| 179 | + |
| 180 | +// Dequeue operations |
| 181 | +let firstElement = queue.shift(); |
| 182 | +console.log(firstElement); // 1 |
| 183 | +console.log([...queue]); // [2, 3] |
| 184 | +``` |
| 185 | + |
| 186 | +::: |
| 187 | + |
| 188 | +## Linked Lists |
| 189 | + |
| 190 | +- **Linked Lists**: Linear data structure where each node contains data and a reference to the next node. Nodes are connected like a chain. |
| 191 | + |
| 192 | +### Singly Linked Lists |
| 193 | + |
| 194 | +- **Structure**: Each node has data and one reference to the next node. |
| 195 | +- **Traversal**: Can only move forward from head to tail. |
| 196 | +- **Head Node**: First node in the list, usually the only directly accessible node. |
| 197 | +- **Tail Node**: Last node in the list, points to `null`. |
| 198 | + |
| 199 | +### Operations and Time Complexities |
| 200 | + |
| 201 | +- **Insert at beginning**: O(1) |
| 202 | +- **Insert at end**: O(n) - must traverse to end |
| 203 | +- **Insert in middle**: O(n) - must traverse to position |
| 204 | +- **Delete from beginning**: O(1) |
| 205 | +- **Delete from end**: O(n) - must traverse to find previous node |
| 206 | +- **Delete from middle**: O(n) - must traverse to find node |
| 207 | + |
| 208 | +### Doubly Linked Lists |
| 209 | + |
| 210 | +- **Structure**: Each node has data and two references: next node and previous node. |
| 211 | +- **Traversal**: Can move in both directions. |
| 212 | +- **Memory**: Requires more memory than singly linked lists due to extra reference. |
| 213 | + |
| 214 | +## When to Use Each Data Structure |
| 215 | + |
| 216 | +- **Arrays (dynamic arrays)**: When you need ordered, indexed access and don't know size in advance |
| 217 | +- **Stacks**: For LIFO operations (undo functionality, expression evaluation, backtracking) |
| 218 | +- **Queues**: For FIFO operations (task scheduling, breadth-first search) |
| 219 | +- **Linked Lists**: When frequent insertion/deletion at beginning, unknown size, no random access needed |
| 220 | + |
| 221 | +# --assignment-- |
| 222 | + |
| 223 | +Review the Data Structures topics and concepts. |
0 commit comments