Beyond the Surface: A Solution-Aware Retrieval Model for Competition-level Code Generation

Shiwen Zhang, Lingxiang Wang, Hainan Zhang, Ziwei Wang, Sijia Wen, Zhi‐Ming Zheng

TL;DR

Competitive programming models often get "distracted" by the narrative flavor text of a problem (e.g., Alice and her candy boxes) rather than focusing on the underlying algorithm. SolveRank is a new retrieval-augmented generation (RAG) framework that uses synthetic data to train a retriever to recognize logical equivalence over surface-level keywords, significantly boosting the performance of LLMs on difficult coding tasks.

What Problem Does This Paper Address?

In competitive programming, problem statements are rarely straightforward. They are usually wrapped in elaborate stories—narrative "fluff" that describes characters, settings, and scenarios. While humans can eventually strip away the story to find the core algorithmic challenge (e.g., "this is just a shortest-path problem"), Large Language Models (LLMs) often struggle.

Current Retrieval-Augmented Generation (RAG) systems for code typically rely on surface-level semantic similarity. If a problem mentions a "store" and "prices," a standard retriever might pull up another problem about a "store," even if the first requires a binary search and the second requires dynamic programming. This "semantic trap" leads models to retrieve irrelevant code examples, which can actually degrade their performance on complex tasks.

Methods

The authors introduce SolveRank, a solution-aware ranking model designed to identify problems that share the same underlying logic, regardless of their narrative description.

  1. Synthetic Data Generation: To train a model to recognize logic over language, the authors needed a dataset of problems that are logically identical but narratively different. They used DeepSeek-R1 to generate five "logically equivalent" variants for each anchor problem in their training set.
  2. Automated Verification: To ensure the synthetic problems didn't drift from the original logic, they used GPT-4o as a judge to verify that the core algorithmic requirements remained the same.
  3. Contrastive Learning: They trained a Dense Passage Retrieval (DPR) model using these synthetic pairs as "positive" samples. For "negative" samples (distractors), they used problems retrieved by BM25 (keyword matching) and random samples. By using InfoNCE loss, they forced the model to push logically similar problems closer together in the embedding space while pushing narratively similar (but logically different) ones apart.
  4. Solution-Aware RAG: During inference, SolveRank retrieves the most logically relevant problems and their corresponding code. This "gold standard" logic is then provided as context to a downstream generator (like GPT-4o) to help it write the final code.

Datasets

  • xCodeEval: The primary benchmark used for training and evaluation, specifically focusing on competitive programming problems in Python.
  • APPS: Used to test the model's ability to generalize to different types of programming challenges.
  • Synthetic Logic Dataset: A newly created set of logically equivalent problem variants generated via DeepSeek-R1 and verified by GPT-4o.

Key Insights and Ideas

  • The "Semantic Trap": The paper highlights that Problems 1 and 2 might both be about "buying gifts in a store," but Problem 1 is a binary search task while Problem 2 is a 0/1 Knapsack problem. Standard retrievers fail here, but SolveRank succeeds by ignoring the "store" context.
  • RAG is a Double-Edged Sword: For "Easy" problems (difficulty $\le$ 1400), adding retrieved context actually decreased performance. The authors suggest that for simple tasks, the extra information acts as a distraction or "cognitive load."
  • Logic Over Keywords: SolveRank achieved a massive 406% increment in Mean Reciprocal Rank (MRR) compared to traditional semantic retrievers, proving that "solution-awareness" is a distinct and learnable feature.

Key Results

  • Retrieval Accuracy: SolveRank dominated SOTA baselines. It achieved a Precision@1 (P@1) of 0.682, compared to just 0.131 for BM25 and 0.096 for CodeBERT.
  • Code Generation (Pass@1): On "Hard" problems (difficulty > 2000), SolveRank boosted GPT-4o’s performance to a 16.67% pass@1 rate, up from 13.89% without retrieval.
  • Consistency: Unlike other models that fluctuate based on wording, SolveRank showed high stability in identifying the correct algorithmic class (e.g., Dynamic Programming, Graph Theory) across different narrative backgrounds.

Limitations and Future Work

  • Easy Problem Degradation: As noted, the model currently struggles to decide when to use retrieval. Future work could involve a "router" that only triggers SolveRank for high-difficulty problems.
  • Math-Heavy Problems: The authors found that for highly abstract math problems, additional reference examples provide limited benefit because success depends more on the generator's internal reasoning than on seeing a similar example.
  • Reinforcement Learning: The team suggests that future iterations could use reinforcement learning to further refine the ranking process based on whether the retrieved code actually helps the generator pass test cases.

Summary updated

BibTeX
@misc{zhang2025beyond,
  author = {Shiwen Zhang and Lingxiang Wang and Hainan Zhang and Ziwei Wang and Sijia Wen and Zhi‐Ming Zheng},
  title = {{Beyond the Surface: A Solution-Aware Retrieval Model for Competition-level Code Generation}},
  year = {2025},
  eprint = {2509.01129},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2509.01129},
}
Figure