TL;DR
Large Language Models (LLMs) can easily write code that satisfies basic unit tests, but they often fail competitive programming benchmarks by exceeding strict time and memory limits. SwiftSolve introduces a multi-agent framework that combines algorithmic planning, deterministic static pruning, sandboxed empirical profiling, and log–log complexity fitting to iteratively optimize code for both correctness and asymptotic efficiency. On a benchmark of 26 competitive programming problems across 3 seeds (78 runs), SwiftSolve boosted cumulative solve rates from 61.54% on the first attempt to 80.77% within three attempts.
What Problem Does This Paper Address?
In competitive programming, functionally "correct" code is not enough. Solutions must strictly respect wall-clock execution limits (typically ~2 seconds) and memory budgets (256–512 MB). Standard LLM code generators often produce naive algorithms—such as $O(n^2)$ nested loops instead of $O(n \log n)$ divide-and-conquer—triggering Time Limit Exceeded (TLE) or Memory Limit Exceeded (MLE) verdicts.
While existing multi-agent coding systems iteratively generate, test, and patch code for logical correctness, they rarely profile or model asymptotic resource growth. SwiftSolve addresses this gap by treating efficiency as a primary optimization objective within an automated, multi-agent repair loop.
Methods
SwiftSolve structures the code generation lifecycle into a modular, message-passing pipeline coordinated by a controller using Pydantic-validated, versioned JSON envelopes:
- Planner (Claude 4 Opus): Transforms a natural language problem into an algorithmic strategy (e.g., dynamic programming, two pointers), extracting input size bounds ($n$) and resource constraints.
- Deterministic Static Pruner: A fast rule-based gate that intercepts the plan before any code generation. It rejects provably disastrous strategies based on declared bounds (e.g., rejecting unbounded recursion when $n \ge 10^4$ or sorting inside loops when $n \ge 10^3$).
- Coder (GPT-4.1): Generates single-translation-unit ISO C++17 source code or applies targeted patches based on downstream feedback.
- Sandboxed Profiler: Compiles candidates with
-O2and runs them against a deterministic, log-spaced input size schedule ($\langle 0, 1, 10^3, \dots, 10^5 \rangle$) inside a POSIX sandbox, tracking execution time and peak resident memory (RSS). - Complexity Analyst: Fits empirical runtimes in log–log space ($\log t = s \log n + c$). If the slope $s$ clearly indicates polynomial complexity ($\le O(n \log n)$), the code is marked efficient. Ambiguous fits fall back to an LLM query. If inefficient, it routes actionable patch directives to the Coder (for micro-optimizations) or triggers an algorithmic replan via the Planner.
The loop runs for up to three attempts (replan_0, replan_1, replan_2), halting early if diminishing runtime returns or fatal exceptions occur.
Problem Prompt
└──> Planner
└──> Static Pruner (rejection gate)
└──> Coder (C++17)
└──> Sandboxed Profiler (empirical scaling)
└──> Complexity Analyst
└──> (Feedback to Coder / Planner)
Datasets
The authors evaluated SwiftSolve on 26 competitive programming tasks:
- BigO(Bench): 16 algorithmic tasks covering foundational patterns (e.g., binary search, segment trees, Dijkstra, sliding window).
- Codeforces Div. 2: 10 real-world contest problems (e.g., CF1285 problems).
Experiments used 3 random seeds per task ($N = 78$ total task–seed trials) evaluated under standard contest constraints (2.0 s time limit; 256–512 MB memory).
Key Insights and Ideas
- Inefficiency, not logic, is the bottleneck: In evaluation, 100% of failed runs (21 of 78) failed due to resource limits (16 TLE, 5 MLE) rather than logical errors or crashes.
- Cheap deterministic filtering saves expensive LLM calls: The Static Pruner catches algorithmic mismatches upfront before any tokens are spent on code synthesis.
- Dynamic empirical profiling guides precise repair: Fitting empirical curves across input sizes pinpoints whether code needs minor implementation tweaks (e.g., avoiding dynamic reallocations) or a complete algorithmic pivot.
- High ROI on iterative replanning: Moving from a single attempt to three attempts yielded substantial accuracy jumps with almost negligible per-attempt latency growth (11.96 s vs. 12.66 s).
Key Results
- Success Rates:
- PASS@1 (first attempt): 61.54% (16/26 tasks).
- SOLVED@$\le$3 (cumulative): Reached 80.77% by attempt 3 (76.92% at attempt 2).
- Aggregate Run-Level Success: 73.08% (57/78 completed trials) with an average run duration of 12.40 s.
- Dataset Comparison: BigO(Bench) tasks had a higher success rate (77.55%, 10.30 s mean) compared to the more complex Codeforces problems (65.52%, 15.95 s mean).
- Baseline Comparison: SwiftSolve significantly outperformed direct single-pass baselines:
- Direct GPT-4.1: ~60.4% on BigO, 44.4% on Codeforces.
- Direct Claude 4 Opus: ~62.5% on BigO, 46.7% on Codeforces (52.6% run-level overall).
- SwiftSolve reached 73.1% overall success at roughly $\sim 2\times$ the runtime overhead (12.4 s vs. 6.8 s).
Limitations and Future Work
- Task-Agnostic Input Generator: The profiler currently feeds synthetic inputs containing only integer $n$. This misses branch-dependent or input-distribution worst cases (e.g., adversarial quicksort pivot patterns, skewed graph topologies).
- Small Benchmark Scale: The test suite was limited to 26 problems across C++17 due to compute budgets.
- Discretization and Heuristic Sensitivity: Log–log slope classification can become unstable at smaller values of $n$, and replanning budgets rely on fixed thresholds rather than task-adaptive confidence.
- Future Directions: Incorporating task-specific adversarial test generators, expanding to multiple programming languages, testing against other multi-agent architectures, and running detailed component ablation studies.
Summary updated