#77Best-of-N SelectionEasyLLMsEvaluation MetricsAsked atOpenAI · Anthropic · Google
Best-of-N Selection
Background
Best-of-N (BoN) is the simplest LLM inference-time scaling pattern: generate candidates, score each with a reward model, return the highest scorer. With a sufficiently good reward model, BoN consistently beats single-shot decoding on coding, math, and instruction-following benchmarks.
Problem statement
Implement best_of_n(candidates, rewards) returning the candidate with the highest reward. Ties are broken by lower original index (deterministic).
Input
candidates— sequence of items (any type). Must be non-empty.rewards— parallel sequence of numeric scores.
Output
Returns the chosen candidate — whatever type the input list contained.
Examples
Example 1 — clear best
Input: candidates=["A","B","C"], rewards=[1, 3, 2]
Output: "B"
Example 2 — tie broken by lower index
Input: candidates=["A","B"], rewards=[5, 5]
Output: "A"
Constraints
candidatesandrewardsmust have the same length. ValueError otherwise.- Empty input raises ValueError.
- Returns the candidate itself, not its index.
Notes
- vs majority vote. BoN uses an external reward signal; self-consistency / majority vote uses only the candidates' agreement. BoN can be tighter when a reliable reward model exists.
- Verifier vs reward model. For verifiable tasks (math, code), the "reward" is a unit-test pass rate or symbolic check — sharper than a learned reward model.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example 1 - clear best
- •Reference tie broken by lower index
- •Sample single candidate