Stage I: Hard Questions

Arrays

Pattern: Two-Pointer Technique

These problems use two indices, often moving towards each other or at different speeds, to solve problems efficiently.

Squares of a Sorted Array

Companies: Facebook (Meta), Amazon, Apple, Google, Microsoft

Concept Tested: A two-pointer approach from both ends. Since the largest squared values will be at the extremes (e.g., `-10` and `10`), you can build the sorted result from the end backwards.

Pattern: Prefix/Suffix Calculation

These problems involve creating a result where result[i] depends on calculations over elements before or after it.

Product of Array Except Self

Companies: Amazon, Facebook (Meta), Microsoft, Apple, Lyft

Concept Tested: A brilliant use of two passes. One pass calculates the product of all elements to the left (prefix), and a second pass from the right calculates the suffix products and combines them.

Pattern: Array as a Map (Cyclic Sort)

These problems use a clever trick where the array's indices are used to store information about its values.

Find All Numbers Disappeared in an Array

Companies: Amazon, Facebook (Meta), Google, Microsoft

Concept Tested: Using the array itself as a hash map. You can mark indices corresponding to the numbers you've seen by making the value at that index negative.

Pattern: Matrix (2D Array) Traversal

This pattern extends the 1D array concept to two dimensions, requiring careful management of two sets of indices (row and column).

Spiral Matrix

Companies: Amazon, Microsoft, Google, Facebook (Meta), Apple

Concept Tested: Managing boundaries (top, bottom, left, right) and adjusting them as you traverse the outer layers of the matrix.

Time Complexity

Square Root Time Complexity: O(√n)

Problem: Analyze the time complexity of the optimized primality test.

Companies: Google, Amazon, Microsoft


def is_prime(n):
    if n <= 1:
        return False
    i = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += 1
    return True

Analysis:

  • Time Complexity: O(√n). The loop runs from 2 up to √n. This is a massive improvement over the naive O(n) approach.
  • Space Complexity: O(1). It uses a fixed amount of memory.

Quadratic Time Complexity: O(n²)

Problem: Analyze the complexity of a function that checks for duplicates using a nested loop.

Concept Tested: Understanding how nested loops dramatically increase complexity.

LeetCode Link: Contains Duplicate (Note: This is the inefficient, brute-force solution).

Companies: Amazon, Apple, Facebook (Meta), Microsoft, Adobe


def has_duplicate_brute_force(numbers):
    for i in range(len(numbers)):
        for j in range(i + 1, len(numbers)):
            if numbers[i] == numbers[j]:
                return True
    return False

Analysis:

  • Time Complexity: O(n²). The outer loop runs n times, and the inner loop runs roughly n times for each outer loop iteration.
  • Space Complexity: O(1). No new data structure that scales with n is created.

Analyzing a Real Algorithm: Binary Search

Problem: Analyze the time and space complexity of Binary Search on a sorted array.

Concept Tested: Connecting the O(log n) analogy to its most famous implementation.

LeetCode Link: Binary Search

Companies: Amazon, Microsoft, Facebook (Meta), Google, Apple, Adobe


def binary_search(sorted_array, target):
    low = 0
    high = len(sorted_array) - 1
    while low <= high:
        mid = (low + high) // 2
        if sorted_array[mid] == target:
            return mid
        elif sorted_array[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1 # Not found

Analysis:

  • Time Complexity: O(log n). At each step, we discard half of the remaining search space.
  • Space Complexity: O(1). This iterative version uses a fixed number of variables.

Recursive Space Complexity

Problem: Analyze the time and space complexity of a recursive function to calculate a sum.

Concept Tested: Understanding that the call stack contributes to space complexity.


def recursive_sum(numbers, index):
    # Base case: we've gone past the end of the array
    if index >= len(numbers):
        return 0
    # Recursive step
    return numbers[index] + recursive_sum(numbers, index + 1)

Analysis:

  • Time Complexity: O(n). The function calls itself once for each of the n elements.
  • Space Complexity: O(n). Each recursive call adds a new layer ("stack frame") to memory. For an array of size n, there will be n active calls on the stack at the deepest point.

Divide and conquer

🧠 Recursion Fundamentals

Generate Parentheses

Concept Tested: Deeper recursion with multiple branches (backtracking). You build a solution step-by-step and "pass down" the current state.

Companies: Amazon, Google, Facebook (Meta), Microsoft, Uber

🔎 Binary Search and its Variants

Find Minimum in Rotated Sorted Array

Concept Tested: Modifying the Binary Search comparison logic. You're not comparing with a target, but with another element in the array to decide which half is sorted.

Companies: Microsoft, Amazon, Facebook (Meta), Google, Apple

Median of Two Sorted Arrays

Concept Tested: The pinnacle of Binary Search problems. Instead of searching for a value, you perform a binary search on the partitions of one array to find the perfect cut that divides the combined elements into two equal halves.

Companies: Google, Amazon, Microsoft, Apple, Adobe, Goldman Sachs

🌊 Merge Sort and its Applications

Count of Smaller Numbers After Self (Inversion Count)

Concept Tested: Augmenting the merge step of Merge Sort. While merging, if you take an element from the right half, you know it's smaller than all remaining elements in the left half, allowing you to count "inversions."

Companies: Google, Amazon, Microsoft, Oracle

⚡ Quick Sort and its Applications (Partitioning)

Kth Largest Element in an Array (Quickselect)

Concept Tested: The classic application of Quick Sort's partition logic. Instead of recurring on both sides, you only recur on the side where the Kth element must be. This reduces the average time complexity from O(n log n) to O(n).

Companies: Facebook (Meta), Amazon, Apple, Microsoft, Google