← Back to Course Overview

Code-Alongs

Code-Along 1: Introduction to LeetCode and Basic Data Types

In this code-along, you'll learn how to navigate LeetCode and work with basic data types in JavaScript.

Code-Along 2: Problem Solving with JavaScript

Join us for a live coding session where we'll solve various programming problems using JavaScript.

Problem-Solving Approaches and Patterns

Developing a systematic approach to problem-solving is crucial for tackling coding challenges. Here's a framework to help you approach any coding problem:

The UMPIRE Method

  1. Understand the problem - Read carefully, identify inputs/outputs, clarify constraints
  2. Match the problem to similar ones - Recognize patterns from problems you've seen before
  3. Plan your approach - Design an algorithm before coding
  4. Implement your solution - Convert your plan to code
  5. Review your code - Look for bugs, edge cases, or optimizations
  6. Evaluate your solution - Analyze time and space complexity

Common Problem Types and Approaches

1. String Manipulation Problems

Example: Valid Palindrome

Problem: Determine if a string is a palindrome, considering only alphanumeric characters and ignoring case.

Approach:

  1. Remove non-alphanumeric characters and convert to lowercase
  2. Compare the string with its reverse
function isPalindrome(s) {
  // Remove non-alphanumeric and convert to lowercase
  const cleanString = s.toLowerCase().replace(/[^a-z0-9]/g, '');
  
  // Check if it reads the same forward and backward
  return cleanString === cleanString.split('').reverse().join('');
}

console.log(isPalindrome("A man, a plan, a canal: Panama")); // true
console.log(isPalindrome("race a car")); // false

2. Array Problems

Example: Two Sum

Problem: Given an array of numbers and a target sum, find two numbers that add up to the target.

Approach:

  1. Naive: Use nested loops to check all pairs (O(n²) time)
  2. Optimal: Use a hash map to store differences (O(n) time)
function twoSum(nums, target) {
  // Create a map to store values we've seen and their indices
  const map = new Map();
  
  for (let i = 0; i < nums.length; i++) {
    // Calculate the complement needed to reach target
    const complement = target - nums[i];
    
    // If we've seen the complement before, we found our pair
    if (map.has(complement)) {
      return [map.get(complement), i];
    }
    
    // Store current number and its index
    map.set(nums[i], i);
  }
  
  return null; // No solution found
}

console.log(twoSum([2, 7, 11, 15], 9)); // [0, 1] (2 + 7 = 9)

3. Number Manipulation Problems

Example: FizzBuzz

Problem: Print numbers from 1 to n, but for multiples of 3 print "Fizz", for multiples of 5 print "Buzz", and for multiples of both print "FizzBuzz".

Approach:

  1. Loop through numbers 1 to n
  2. Use modulo operator to check divisibility
  3. Apply conditional logic in the right order
function fizzBuzz(n) {
  const result = [];
  
  for (let i = 1; i <= n; i++) {
    // Check divisibility by both 3 and 5 first
    if (i % 3 === 0 && i % 5 === 0) {
      result.push("FizzBuzz");
    } 
    // Then check divisibility by 3
    else if (i % 3 === 0) {
      result.push("Fizz");
    } 
    // Then check divisibility by 5
    else if (i % 5 === 0) {
      result.push("Buzz");
    } 
    // Otherwise just add the number
    else {
      result.push(i.toString());
    }
  }
  
  return result;
}

console.log(fizzBuzz(15)); 
// [1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz]

Problem-Solving Techniques

Optimizing Your Solutions

When you've solved a problem, always look for ways to optimize:

  1. Time Complexity: Reduce the number of operations needed
  2. Space Complexity: Minimize memory usage
  3. Code Readability: Make your code clearer with descriptive variable names and comments
  4. Edge Cases: Ensure your solution handles all possible inputs
  5. Error Handling: Add appropriate error checks for invalid inputs

Practice Problem: Contains Duplicate

Problem: Given an integer array, return true if any value appears at least twice, and false if every element is distinct.

Example:

Input: [1,2,3,1]
Output: true

Input: [1,2,3,4]
Output: false

Try to solve this problem on your own first, then check the solution below:

function containsDuplicate(nums) {
  // Option 1: Using a Set
  return new Set(nums).size !== nums.length;
  
  /* Option 2: Using an object
  const seen = {};
  for (let num of nums) {
    if (seen[num]) return true;
    seen[num] = true;
  }
  return false;
  */
}

console.log(containsDuplicate([1,2,3,1])); // true
console.log(containsDuplicate([1,2,3,4])); // false

Additional Resources