Abstract
Code generation aims to automatically generate source code from high-level task specifications, which can significantly increase productivity of software engineering. Recently, approaches based on large language models (LLMs) have shown remarkable code generation abilities on simple tasks. However, generating code for more complex tasks, such as competition-level problems, remains challenging. In this paper, we introduce the Brainstorm framework for code generation. It leverages a brainstorming step that generates and selects diverse thoughts on the problem to facilitate algorithmic reasoning, where the thoughts are possible blueprints for solving the problem. We demonstrate that Brainstorm significantly enhances the ability of LLMs to solve competition-level programming problems, resulting in a more than 50% increase in the pass@k metrics for ChatGPT on the CodeContests benchmark, achieving state-of-the-art performance. Furthermore, our experiments conducted on LeetCode contests show that our framework boosts the ability of ChatGPT to a level comparable to that of human programmers.
Introduction
While Large Language Models (LLMs) like GPT-4 excel at generating short code snippets or solving introductory programming tasks, they often struggle with competition-level problems. These complex tasks require deep algorithmic reasoning, understanding intricate constraints, and planning a multi-step strategy. Current approaches often rely on Reinforcement Learning or massive sampling, which are computationally expensive and lack a high-level blueprint. The authors argue that LLMs fail here because they attempt to "code and think" simultaneously, whereas human experts brainstorm a strategy before implementation.
The BRAINSTORM Framework
The BRAINSTORM framework breaks code generation into three distinct stages to decouple strategy from syntax:
- Brainstorming: Instead of immediate code generation, the LLM is prompted with the problem description and specific instruction sets (e.g., "analyze time complexity" or "act as a teacher"). This produces a variety of "thoughts"—high-level algorithmic blueprints in natural language.
- Thoughts Selection: A Neural Ranker (based on the XLNet architecture) evaluates the problem description alongside the generated thoughts to predict which strategy is most likely to lead to a correct solution.
- Writing Code: The LLM receives the original problem plus the selected "best thought" to implement the final code.
Experimental Setup and Results
The framework was evaluated on three major benchmarks: CodeContests, APPS, and LeetCode. To train the ranker, the authors curated specialized datasets of correct and incorrect examples for CodeContests and APPS.
The BRAINSTORM framework yielded significant performance gains, particularly on difficult tasks. On the CodeContests benchmark, the pass@1 metric for ChatGPT improved from 4.5% to 7.0%, representing a 55.1% relative increase.
Key Insights
- Strategic Decoupling: The bottleneck in code generation is often strategy rather than syntax. Decoupling "what to do" from "how to write it" allows the LLM to focus more effectively.
- Diversity of Thought: Generating multiple types of thoughts using different prompts is more effective than simple repetitive sampling.
- Complexity Scaling: Relative improvements increase as problem difficulty rises, suggesting the framework provides genuine algorithmic reasoning support.
- The "Over-thinking" Penalty: For very simple tasks, brainstorming can occasionally lead to over-engineering, slightly hurting performance compared to direct generation.
Conclusion and Future Work
BRAINSTORM demonstrates that structured reasoning can elevate LLM performance to human-competitive levels on platforms like LeetCode. However, challenges remain regarding input length limits for extremely complex problems and the domain shift required for rankers to generalize across different coding platforms. Future research may explore integrating the ranker directly into the LLM or developing more efficient pruning methods for the brainstorming phase.
Summary updated