Common Mistakes and Pitfalls

🚨 Time Complexity Mistakes

āŒ Nested Loops = O(n²)

Mistake: Thinking nested loops are O(n) Correct: Nested loops are O(n²) or worse Example:

# Wrong: O(n)
for i in range(n):
    for j in range(n):
        # operations

Fix: Look for ways to reduce nested loops

āŒ String Operations = O(1)

Mistake: Thinking string concatenation is O(1) Correct: String operations are often O(n) Example:

# Wrong: O(1)
result = result + "string"  # O(n) each time

# Correct: O(n)
result = "".join(strings)   # O(n) total

āŒ List Operations = O(1)

Mistake: Thinking list operations are O(1) Correct: Many list operations are O(n) Example:

# Wrong: O(1)
list.insert(0, item)  # O(n)
list.pop(0)           # O(n)

# Correct: O(1)
list.append(item)     # O(1) amortized
list.pop()            # O(1)

šŸ’¾ Memory Issues

āŒ Large Arrays Without Optimization

Mistake: Creating large arrays when not needed Correct: Use sparse representations Example:

# Wrong: O(n²) space
dp = [[0] * n for _ in range(n)]

# Correct: O(n) space
dp = [0] * n  # if only current row needed

āŒ Deep Recursion

Mistake: Deep recursion causing stack overflow Correct: Use iterative approach or tail recursion Example:

# Wrong: Stack overflow for large n
def factorial(n):
    if n <= 1: return 1
    return n * factorial(n-1)

# Correct: Iterative
def factorial(n):
    result = 1
    for i in range(1, n+1):
        result *= i
    return result

āŒ String Concatenation in Loops

Mistake: Building strings with + in loops Correct: Use list and join Example:

# Wrong: O(n²)
result = ""
for i in range(n):
    result += str(i)

# Correct: O(n)
result = "".join(str(i) for i in range(n))

āš ļø Edge Cases

āŒ Empty Input

Mistake: Not checking for empty input Correct: Always check edge cases Example:

# Wrong: Crashes on empty array
def max_element(arr):
    return max(arr)

# Correct: Handle empty case
def max_element(arr):
    if not arr:
        return None
    return max(arr)

āŒ Single Element

Mistake: Not handling single element case Correct: Check for single element Example:

# Wrong: May fail for single element
def find_second_max(arr):
    return sorted(arr)[-2]

# Correct: Handle single element
def find_second_max(arr):
    if len(arr) < 2:
        return None
    return sorted(arr)[-2]

āŒ Negative Numbers

Mistake: Not considering negative numbers Correct: Check bounds and signs Example:

# Wrong: May fail with negatives
def sqrt_floor(n):
    return int(n ** 0.5)

# Correct: Handle negatives
def sqrt_floor(n):
    if n < 0:
        return None
    return int(n ** 0.5)

āŒ Integer Overflow

Mistake: Not handling large numbers Correct: Use appropriate data types Example:

# Wrong: Overflow for large numbers
def factorial(n):
    result = 1
    for i in range(1, n+1):
        result *= i
    return result

# Correct: Use modular arithmetic
def factorial_mod(n, mod):
    result = 1
    for i in range(1, n+1):
        result = (result * i) % mod
    return result

šŸ” Algorithm Selection Mistakes

āŒ Wrong Algorithm for Constraints

Mistake: Using O(n²) for n ≤ 10⁶ Correct: Choose algorithm based on constraints Example:

# Wrong: O(n²) for large n
def find_pair_sum(arr, target):
    for i in range(len(arr)):
        for j in range(i+1, len(arr)):
            if arr[i] + arr[j] == target:
                return (i, j)
    return None

# Correct: O(n) with hash set
def find_pair_sum(arr, target):
    seen = set()
    for i, num in enumerate(arr):
        complement = target - num
        if complement in seen:
            return (arr.index(complement), i)
        seen.add(num)
    return None

āŒ Over-engineering Simple Problems

Mistake: Using complex algorithms for simple problems Correct: Start simple, optimize if needed Example:

# Wrong: Complex DP for simple problem
def sum_array(arr):
    dp = [0] * len(arr)
    dp[0] = arr[0]
    for i in range(1, len(arr)):
        dp[i] = dp[i-1] + arr[i]
    return dp[-1]

# Correct: Simple solution
def sum_array(arr):
    return sum(arr)

šŸ”¢ Mathematical Mistakes

āŒ Division by Zero

Mistake: Not checking for division by zero Correct: Always check denominator Example:

# Wrong: Division by zero
def average(arr):
    return sum(arr) / len(arr)

# Correct: Check for empty array
def average(arr):
    if not arr:
        return 0
    return sum(arr) / len(arr)

āŒ Floating Point Precision

Mistake: Using floating point for exact comparisons Correct: Use integer arithmetic when possible Example:

# Wrong: Floating point comparison
def is_equal(a, b):
    return abs(a - b) < 1e-9

# Correct: Integer comparison
def is_equal(a, b):
    return a == b

āŒ Modulo with Negative Numbers

Mistake: Incorrect modulo with negative numbers Correct: Handle negative numbers properly Example:

# Wrong: Incorrect for negative numbers
def mod(a, m):
    return a % m

# Correct: Handle negatives
def mod(a, m):
    return ((a % m) + m) % m

šŸŽÆ Prevention Strategies

šŸ“‹ Before Coding

  1. Read constraints carefully
  2. Identify edge cases
  3. Choose appropriate algorithm
  4. Plan implementation steps

šŸ“‹ During Coding

  1. Check bounds and edge cases
  2. Use appropriate data types
  3. Test with small examples
  4. Verify time complexity

šŸ“‹ After Coding

  1. Test with edge cases
  2. Check for overflow
  3. Verify correctness
  4. Optimize if needed

šŸš€ Best Practices

āœ… Always Do

  • Check input constraints
  • Handle edge cases
  • Use appropriate data types
  • Test with examples
  • Verify time complexity

āŒ Never Do

  • Assume input is valid
  • Ignore edge cases
  • Use wrong data types
  • Skip testing
  • Ignore time complexity

šŸ“Š Common Complexity Mistakes

ā° Time Complexity

  • Nested loops: O(n²) not O(n)
  • String operations: O(n) not O(1)
  • List operations: O(n) not O(1)
  • Sorting: O(n log n) not O(n)

šŸ’¾ Space Complexity

  • Recursion: O(depth) not O(1)
  • Large arrays: O(n²) not O(n)
  • String building: O(n²) not O(n)
  • Hash maps: O(n) not O(1)