// Use these by default#define int long long // Prevent overflow// Check array bounds explicitlyif (i >= 0 && i < n && j >= 0 && j < m) { // Safe to access grid[i][j]}// Initialize variablesint sum = 0; // Not just: int sum;// Use const for array sizesconst int N = 2e5 + 5;int arr[N];
#ifdef LOCAL#define debug(x) cerr << #x << " = " << x << endl#else#define debug(x)#endif// Usageint ans = calculate(n);debug(ans); // Only prints when compiled with -DLOCAL
// BUG: Overflow in multiplicationint a = 100000, b = 100000;int product = a * b; // Overflow!// FIX: Use long longlong long product = (long long)a * b;// Or use #define#define int long long
// BUG: Accessing arr[n] (out of bounds)for (int i = 0; i <= n; i++) { arr[i] = 0; // When i=n, this is out of bounds!}// FIX: Use < instead of <=for (int i = 0; i < n; i++) { arr[i] = 0;}
// BUG: 1-indexed vs 0-indexed confusionfor (int i = 1; i <= n; i++) { cin >> arr[i]; // Works if arr has size n+1}// Later...for (int i = 0; i < n; i++) { // arr[i] has garbage for i=0!}// FIX: Be consistent, prefer 0-indexed
// BUG: Not resetting between test casesint globalCounter = 0;vector<int> seen;void solve() { // globalCounter and seen retain old values!}// FIX: Reset at start of each test casevoid solve() { int counter = 0; // Local variable vector<int> seen; // Fresh vector // Or explicitly clear: seen.clear();}
Stress testing = Running your solution against a brute force on random inputs until they disagree.
┌─────────────────────────────────────────────────────┐│ STRESS TEST FLOW │├─────────────────────────────────────────────────────┤│ 1. Write brute force solution (slow but correct) ││ 2. Write optimized solution (fast but buggy?) ││ 3. Generate random test case ││ 4. Run both solutions ││ 5. Compare outputs ││ 6. If different → found the bug! ││ 7. Repeat until confident │└─────────────────────────────────────────────────────┘
#include <bits/stdc++.h>using namespace std;int main() { int n; cin >> n; vector<int> arr(n); for (int& x : arr) cin >> x; // Your optimized solution int ans = 0; // ... cout << ans << endl; return 0;}
File 2: brute.cpp (simple correct solution)
#include <bits/stdc++.h>using namespace std;int main() { int n; cin >> n; vector<int> arr(n); for (int& x : arr) cin >> x; // Brute force O(n²) or O(n³) - slow but correct int ans = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // Check every possibility } } cout << ans << endl; return 0;}
File 3: gen.cpp (random test generator)
#include <bits/stdc++.h>using namespace std;int main(int argc, char* argv[]) { mt19937 rng(atoi(argv[1])); // Seed from command line int n = rng() % 10 + 1; // n from 1 to 10 (small for stress) cout << n << endl; for (int i = 0; i < n; i++) { cout << (rng() % 100 + 1); // values from 1 to 100 if (i < n-1) cout << " "; } cout << endl; return 0;}
File 4: stress.bat (Windows)
@echo offg++ -O2 solution.cpp -o solution.exeg++ -O2 brute.cpp -o brute.exeg++ -O2 gen.cpp -o gen.exefor /L %%i in (1,1,1000) do ( gen.exe %%i > input.txt solution.exe < input.txt > output1.txt brute.exe < input.txt > output2.txt fc output1.txt output2.txt > nul if errorlevel 1 ( echo Test %%i FAILED! type input.txt echo Solution output: type output1.txt echo Brute output: type output2.txt exit /b 1 ) echo Test %%i passed)echo All tests passed!
File 4: stress.sh (Linux/Mac)
#!/bin/bashg++ -O2 solution.cpp -o solutiong++ -O2 brute.cpp -o bruteg++ -O2 gen.cpp -o genfor i in $(seq 1 1000); do ./gen $i > input.txt ./solution < input.txt > output1.txt ./brute < input.txt > output2.txt if ! diff -q output1.txt output2.txt > /dev/null; then echo "Test $i FAILED!" echo "Input:" cat input.txt echo "Solution output:" cat output1.txt echo "Brute output:" cat output2.txt exit 1 fi echo "Test $i passed"doneecho "All tests passed!"
// Find which test case failsint caseNum = 0;while (t--) { caseNum++; // If answer should be X for case 5: if (caseNum == 5) { cerr << "Debug case 5" << endl; debug(n); debugv(arr); } solve();}