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
- Read constraints carefully
- Identify edge cases
- Choose appropriate algorithm
- Plan implementation steps
š During Coding
- Check bounds and edge cases
- Use appropriate data types
- Test with small examples
- Verify time complexity
š After Coding
- Test with edge cases
- Check for overflow
- Verify correctness
- 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)