Chapter 140

Home

Stress Testing Your Solution

When your solution fails a test but you can't find the bug, stress test: write a slow brute-force, generate random small inputs, and compare outputs. When they differ, you have a minimal failing case.

Stress Test Script (Python)

Python — stress test generator

# gen.py — generates random test case
import random, sys
n = random.randint(1, 10)
arr = [random.randint(1, 100) for _ in range(n)]
print(n)
print(*arr)

# Then: while true; do python gen.py > in.txt
#   ./fast_sol < in.txt > out1.txt
#   ./brute_sol < in.txt > out2.txt
#   diff out1.txt out2.txt || break; done
# When diff fails — you've found your bug!

ðŸ’Ą Keep inputs small

The brute-force should be O(N!) or O(2áīš). Keep N â‰Ī 10 for stress testing. The fast solution handles arbitrary N — you're checking correctness, not performance.