This example demonstrates finding the maximum sum of a contiguous subarray using Kadane’s algorithm.
1
Create the generator
gen.cpp
#include <bits/stdc++.h>using namespace std;int main(int argc, char* argv[]) { // Generator for Maximum Subarray Problem int seed = stoi(string(argv[1])); srand(seed); const int N = int(1e3); const int Ai = int(1e5); int n = rand() % N + 1; cout << n << "\n"; default_random_engine generator; uniform_int_distribution<int> distribution(-Ai, Ai); for(int i = 0; i < n; i++) { if(i > 0) cout << " "; cout << distribution(generator); } return 0;}
The generator creates arrays of random size with positive and negative numbers.
2
Write the brute-force solution
correct.cpp
#include <bits/stdc++.h>using namespace std;int main() { // Maximum Subarray Problem - O(n²) brute force int n; cin >> n; vector<int> values(n); for(int &a: values) cin >> a; int best = 0; for (int i = 0; i < n; i++) { int sum = 0; for (int j = i; j < n; j++) { sum += values[j]; best = max(best, sum); } } cout << best << "\n"; return 0;}
This O(n²) solution is slow but guaranteed correct.
3
Write the optimized solution (with bug)
main.cpp
#include <bits/stdc++.h>using namespace std;int main() { // Maximum Subarray Problem - O(n) with intentional bug int n; cin >> n; vector<int> values(n); for(int &a: values) cin >> a; int best = 0, sum = 0; // Bug: i+1 < n instead of i < n for (int i = 0; i+1 < n; i++) { sum = max(values[i], sum + values[i]); best = max(best, sum); } cout << best << "\n"; return 0;}
This solution has a bug that will be caught by stress testing.
Problem: Given n, output two numbers that multiply to n.
1
Write the checker
checker.cpp
#include <bits/stdc++.h>using namespace std;int main() { // Checker validates that x * y == n // Read n from input file ifstream in (".qt/input.txt"); int n; in >> n; // Read answer from target program's output int x, y; cin >> x >> y; // Validate the answer cout << (x*y == n ? "YES" : "NO") << endl; return 0;}
The checker reads the input from .qt/input.txt and validates the solution’s output.
2
Create the generator
gen.cpp
#include <bits/stdc++.h>using namespace std;int main(int argc, char* argv[]) { int seed = stoi(string(argv[1])); srand(seed); // Generate a random number int n = rand() % 1000 + 1; cout << n << "\n"; return 0;}
3
Write the solution
main.cpp
#include <bits/stdc++.h>using namespace std;int main() { int n; cin >> n; // Find two factors (many valid answers exist) for(int i = 2; i * i <= n; i++) { if(n % i == 0) { cout << i << " " << n/i << "\n"; return 0; } } // n is prime cout << 1 << " " << n << "\n"; return 0;}
# Save all test casesqt stress -t main.cpp -g gen.cpp --tc 1000 --save-all# Analyze which inputs cause slowest execution# Check .qt/testcases/ for patterns
# Run with --diff to see exact differencesqt cmp -t main.cpp -c correct.cpp -g gen.cpp --diff --break# When it stops, check the input/outputcat .qt/input.txtcat .qt/output.txt# Debug with your solutiongdb .qt/main < .qt/input.txt