Code-Alongs - Web Unit 3 Sprint 12

Code-Along Overview

In these code-along sessions, you'll work through practical examples and implementations of the concepts covered in the modules. These hands-on experiences will reinforce your learning and provide you with real-world problem-solving skills essential for technical interviews and professional development.

Code-Along Sessions

1. Big O Analysis and Caching

Learn how to analyze algorithm complexity and implement caching strategies through practical examples.

Key Concepts Covered:

  • Identifying time and space complexity of algorithms
  • Optimizing performance through caching
  • Implementing memoization for recursive functions
  • Comparing algorithms with different complexity classes

Code Example - Fibonacci with Caching:

// Naive recursive implementation - O(2^n) time complexity
function fibonacciSlow(n) {
  if (n <= 1) return n;
  return fibonacciSlow(n - 1) + fibonacciSlow(n - 2);
}

// Optimized implementation with caching - O(n) time complexity
function fibonacciFast(n, memo = {}) {
  // Check if we've already calculated this value
  if (n in memo) return memo[n];
  
  // Base cases
  if (n <= 1) return n;
  
  // Calculate and cache the result
  memo[n] = fibonacciFast(n - 1, memo) + fibonacciFast(n - 2, memo);
  return memo[n];
}

// Compare performance
console.time('Slow Fibonacci');
console.log(fibonacciSlow(30)); // Takes several seconds
console.timeEnd('Slow Fibonacci');

console.time('Fast Fibonacci');
console.log(fibonacciFast(30)); // Nearly instant
console.timeEnd('Fast Fibonacci');

// The performance difference becomes even more dramatic for larger inputs
// fibonacciSlow(50) would take millions of years
// fibonacciFast(50) completes in microseconds
                    

Related Practice: LeetCode: Climbing Stairs - A problem well-suited for memoization.

2. Object References and Linked Lists

Work through examples of object references and linked list implementations.

Key Concepts Covered:

  • Understanding object references vs. primitive values
  • Implementing and manipulating linked list structures
  • Handling edge cases in linked list operations
  • Solving common linked list interview problems

Code Example - Reversing a Linked List:

// Node class for our linked list
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

// Function to create a linked list from an array for testing
function createLinkedList(array) {
  if (array.length === 0) return null;
  
  const head = new Node(array[0]);
  let current = head;
  
  for (let i = 1; i < array.length; i++) {
    current.next = new Node(array[i]);
    current = current.next;
  }
  
  return head;
}

// Function to print a linked list for visualization
function printList(head) {
  const values = [];
  let current = head;
  
  while (current) {
    values.push(current.value);
    current = current.next;
  }
  
  return values.join(' -> ');
}

// Function to reverse a linked list (in-place)
function reverseLinkedList(head) {
  let prev = null;
  let current = head;
  
  while (current) {
    // Save next pointer before we change it
    const next = current.next;
    
    // Reverse the pointer
    current.next = prev;
    
    // Move pointers forward
    prev = current;
    current = next;
  }
  
  // The new head is the previous tail
  return prev;
}

// Test our implementation
const list = createLinkedList([1, 2, 3, 4, 5]);
console.log('Original list:', printList(list)); // 1 -> 2 -> 3 -> 4 -> 5

const reversed = reverseLinkedList(list);
console.log('Reversed list:', printList(reversed)); // 5 -> 4 -> 3 -> 2 -> 1
                    

Related Practice: LeetCode: Reverse Linked List - A classic linked list problem that tests your understanding of references.

3. Classes and Object-Oriented Programming

Key Concepts Covered:

  • Creating and extending JavaScript classes
  • Implementing object-oriented design patterns
  • Building data structures with classes
  • Encapsulating logic and state within classes

Code Example - Stack Implementation:

// Stack implementation using JavaScript class
class Stack {
  constructor() {
    this.items = [];
    this.size = 0;
  }
  
  // Add element to the top of the stack
  push(element) {
    this.items.push(element);
    this.size++;
    return this;
  }
  
  // Remove and return the top element
  pop() {
    if (this.isEmpty()) {
      return null;
    }
    
    this.size--;
    return this.items.pop();
  }
  
  // View the top element without removing it
  peek() {
    if (this.isEmpty()) {
      return null;
    }
    
    return this.items[this.size - 1];
  }
  
  // Check if stack is empty
  isEmpty() {
    return this.size === 0;
  }
  
  // Get the size of the stack
  getSize() {
    return this.size;
  }
  
  // Clear the stack
  clear() {
    this.items = [];
    this.size = 0;
    return this;
  }
}

// Example usage
const stack = new Stack();
stack.push(10).push(20).push(30);

console.log('Stack size:', stack.getSize()); // 3
console.log('Top element:', stack.peek()); // 30

console.log('Popped element:', stack.pop()); // 30
console.log('New top element:', stack.peek()); // 20
console.log('New size:', stack.getSize()); // 2

stack.clear();
console.log('After clearing, is empty?', stack.isEmpty()); // true
                    

Related Practice: LeetCode: Min Stack - A problem that requires implementing a stack data structure with additional functionality.

Additional Resources