Explaining Competitive-Level Programming Solutions using LLMs

Jierui Li, Szymon Tworkowski, Yingying Wu, Raymond J. MooneyarXiv 2023 · DOI

Introduction

Competitive programming represents a "grand challenge" for Artificial Intelligence, requiring deep algorithmic reasoning, mathematical intuition, and precise implementation. While Large Language Models (LLMs) like GPT-4 excel in general reasoning, they often struggle with the "tangled" nature of competitive tasks, where high-level logic and low-level syntax are intertwined. This paper explores a novel approach: using LLMs to generate structured natural language explanations—or "editorials"—to bridge the gap between problem understanding and code generation.

Methodology: The Specific-to-General (S2G) Framework

The core of this research is the Specific-to-General (S2G) prompting framework. This methodology disentangles reasoning from implementation by employing a two-agent system:

  1. The Explainer: This agent (typically GPT-4) analyzes a problem statement alongside a human-written "oracle" solution. It generates a structured explanation that moves from concrete descriptions (what the code does) to abstract analysis (why the algorithm is correct).
  2. The Solver: This agent (GPT-3.5) receives the original problem statement paired with a specific component of the generated explanation as a "hint" to generate the final code.
The Specific-to-General (S2G) prompting framework and agent architecture.

The framework forces the model to analyze solutions at multiple levels of abstraction, including brief summaries, used algorithms, step-by-step descriptions, and proofs of correctness.

Experimental Setup

The study utilized CodeContests, a benchmark featuring problems from platforms like Codeforces. The evaluation set consisted of 165 problems published after the training cutoffs for the models used. Additionally, a human evaluation set of 50 problems was used to gather Likert-scale ratings from expert competitive programmers on the quality of the LLM-generated explanations.

Key Results and Insights

The introduction of structured hints led to a dramatic improvement in performance. On the CodeContests dataset, the baseline solve@10 rate of 6.1% surged to 42.4% when the solver was provided with a "Step-by-Step" explanation.

The Efficacy of Step-by-Step Hints

Not all explanation types are equally effective. The "Step-by-Step Solution Description" emerged as the most potent hint, serving as a vital bridge between abstract logic and concrete code. This approach significantly reduced "Time Limit Exceeded" (TLE) errors, suggesting that natural language explanations guide models toward more efficient algorithmic strategies (e.g., optimizing from $O(n^2)$ to $O(n \log n)$).

Solve rate by problem difficulty (rating) across different hint types, highlighting the dominance of Step-by-Step explanations.

The Explanation-Generation Paradox

A primary finding is that LLMs are significantly better at explaining existing solutions than generating them from scratch. GPT-4, in particular, demonstrated a superior ability to capture the "key idea" behind complex algorithms, receiving an average human approval rating of 1.30/2.0.

Conclusion

While the current framework relies on an "Oracle Dependency" (requiring a human solution to generate the explanation), it demonstrates the power of natural language reasoning in complex coding tasks. Future work aims to eliminate this dependency, allowing LLMs to generate high-quality reasoning steps autonomously, potentially through fine-tuning or iterative self-correction cycles.

Summary updated

BibTeX
@misc{li2023explaining,
  author = {Jierui Li and Szymon Tworkowski and Yingying Wu and Raymond J. Mooney},
  title = {{Explaining Competitive-Level Programming Solutions using LLMs}},
  year = {2023},
  eprint = {2307.05337},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2307.05337},
}
Figure