curriculum/challenges/english/blocks/review-data-structures-js/699c2dd0e2e1f947791f33fc.md
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.
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.
:::interactive_editor
function checkEvenOrOdd(number) {
if (number % 2 === 0) {
return "Even";
} else {
return "Odd";
}
}
console.log(checkEvenOrOdd(4)) // "Even"
console.log(checkEvenOrOdd(5)) // "Odd"
:::
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).
O(n) - Linear Time: Running time increases proportionally to input size.
for (const grade of grades) { // grades is an array
console.log(grade);
}
O(n log n) - Log-Linear Time: Common time complexity of efficient sorting algorithms like Merge Sort and Quick Sort.
O(n²) - Quadratic Time: Running time increases quadratically. Often seen in nested loops.
:::interactive_editor
const n = 3;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
console.log("Hello, World!");
}
}
:::
Understanding the Problem: Read the problem statement multiple times. Identify the input, expected output, and how to transform input to output.
Pseudocode: High-level description of algorithm logic that is language-independent. Uses common written language mixed with programming constructs like IF, ELSE, FOR, WHILE.
GET original_string
SET reversed_string = ""
FOR EACH character IN original_string:
ADD character TO THE BEGINNING OF reversed_string
DISPLAY reversed_string
Static Arrays: Have a fixed size determined at initialization. Elements stored in adjacent memory locations. Size cannot be changed during program execution.
Dynamic Arrays: Can grow or shrink automatically during program execution. Handle resizing through automatic copying to larger arrays when needed.
:::interactive_editor
let numbers = [3, 4, 5, 6];
// Access elements
console.log(numbers[0]) // 3
// Update elements
numbers[2] = 16
console.log([...numbers]); // [3, 4, 16, 6]
// Add elements
numbers.push(7)
console.log([...numbers]); // [3, 4, 16, 6, 7]
// Add at a specific index
numbers.splice(3, 0, 15);
console.log([...numbers]); // [3, 4, 16, 15, 6, 7]
// Remove elements
numbers.splice(2, 1); // Remove at specific index
console.log([...numbers]); // [3, 4, 15, 6, 7]
numbers.pop() // Remove last element
console.log([...numbers]); // [3, 4, 15, 6]
:::
Stacks: Last-In, First-Out (LIFO) data structure. Elements added and removed from the top only.
Push Operation: Adding an element to the top of the stack. Time complexity: O(1).
Pop Operation: Removing an element from the top of the stack. Time complexity: O(1).
:::interactive_editor
// Using JavaScript array as a stack
let stack = [];
// Push operations
stack.push(1);
stack.push(2);
stack.push(3);
console.log([...stack]); // [1, 2, 3]
// Pop operations
let topElement = stack.pop();
console.log(topElement); // 3
console.log([...stack]); // [1, 2]
:::
Queues: First-In, First-Out (FIFO) data structure. Elements added to the back and removed from the front.
Enqueue Operation: Adding an element to the back of the queue. Time complexity: O(1).
Dequeue Operation: Removing an element from the front of the queue. Time complexity: O(1).
:::interactive_editor
// Using JavaScript array as a queue
let queue = [];
// Enqueue operations
queue.push(1);
queue.push(2);
queue.push(3);
console.log([...queue]); // [1, 2, 3]
// Dequeue operations
let firstElement = queue.shift();
console.log(firstElement); // 1
console.log([...queue]); // [2, 3]
:::
null.Review the Data Structures topics and concepts.