Best LLM for Coding in 2026
For coding, GLM-5.2 is our pick: $2.00/M tokens on a Coding agent loop workload, 1M context, graded 100/100 across 1 run.
Coding assistants live or die on correctness — a fast, cheap answer that hides a subtle bug costs more time than it saves. We weight this task on graded output quality first, with price and speed as tie-breakers, not the other way around.
What is the best LLM for coding?
GLM-5.2, from Z.ai, is the best fit for coding at $2.00 per million task tokens on a Coding agent loop workload, with a 1M-token context window. No cheaper value pick beats it for this task.
Can't use GLM-5.2? See GLM-5.2 alternatives.
Evidence
Graded on 16 of 32 eligible models, run 2026-06-21. Full prompts, verbatim outputs, and grading notes below.
Writing a Code Snippet
A focused coding task: produce a correct, efficient, 0-indexed iterative Fibonacci function in Python — and nothing but the code.
| # | Model | Accuracy ↓ | Speed ↕ | Cost ↕ | Output |
|---|---|---|---|---|---|
| 1 | GPT-5.4 NanoOpenAI · gpt-5.4-nano | 100★ | 44.2 t/s1742 ms | $0.000109$1.25/M out | |
Agent grade — 100/100. Correct iterative algorithm; fib(0)=0 and fib(1)=1 verified. Clean, code-only, and adds a sensible negative-input guard. Verbatim output (66 in / 77 out tokens, 1742 ms, $0.000109) ```python
def fib(n):
if n < 0:
raise ValueError("n must be non-negative")
if n == 0:
return 0
a, b = 0, 1
for _ in range(1, n):
a, b = b, a + b
return b
``` | |||||
| 2 | Llama 3.3 70BGroq · llama-3.3-70b | 99 | 198 t/s298 ms | $0.000103$0.79/M out | |
Agent grade — 99/100. Correct and elegant (returns n for n<=1). Clean and code-only. Verbatim output (95 in / 59 out tokens, 298 ms, $0.000103) ```python
def fib(n):
if n <= 1:
return n
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
| 3 | Llama 3.1 8BGroq · llama-3.1-8b | 99 | 409.7 t/s144 ms | $0.0000095$0.08/M out | |
Agent grade — 99/100. Correct, clean, code-only — and the fastest response in this test. Verbatim output (95 in / 59 out tokens, 144 ms, $0.0000095) ```python
def fib(n):
if n <= 1:
return n
a, b = 0, 1
for i in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
| 4 | Mistral Medium 3Mistral · mistral-medium | 99 | 36.8 t/s1629 ms | $0.00015$2/M out | |
Agent grade — 99/100. Correct, concise, and strictly code-only. No reasoning leakage. Verbatim output (76 in / 60 out tokens, 1629 ms, $0.00015) ```python
def fib(n):
if n == 0:
return 0
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
| 5 | Mistral Small 3.1Mistral · mistral-small | 99 | 95.4 t/s629 ms | $0.0000474$0.6/M out | |
Agent grade — 99/100. Correct, concise, code-only — identical quality to the larger Mistral at a fraction of the cost. Verbatim output (76 in / 60 out tokens, 629 ms, $0.0000474) ```python
def fib(n):
if n == 0:
return 0
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
| 6 | Ministral 8BMistral · ministral-8b | 99 | 43.9 t/s1368 ms | $0.0000186$0.15/M out | |
Agent grade — 99/100. Correct, code-only, and one of the cheapest runs in the test. Verbatim output (64 in / 60 out tokens, 1368 ms, $0.0000186) ```python
def fib(n):
if n == 0:
return 0
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
| 7 | CodestralMistral · codestral | 99 | 91.9 t/s653 ms | $0.0000732$0.9/M out | |
Agent grade — 99/100. Correct, code-only — exactly what you want from a code-specialised model. Verbatim output (64 in / 60 out tokens, 653 ms, $0.0000732) ```python
def fib(n):
if n == 0:
return 0
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
| 8 | Llama 4 ScoutGroq · llama-4-scout | 98 | 218 t/s344 ms | $0.0000332$0.34/M out | |
Agent grade — 98/100. Correct with explicit base cases, code-only. No issues. Verbatim output (70 in / 75 out tokens, 344 ms, $0.0000332) ```python
def fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
| 9 | Amazon Nova LiteAmazon · nova-lite | 98 | 133.5 t/s562 ms | $0.0000219$0.24/M out | |
Agent grade — 98/100. Correct, code-only, clean explicit base cases. Verbatim output (65 in / 75 out tokens, 562 ms, $0.0000219) ```python
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
| 10 | Gemini 3.1 Flash LiteGoogle · gemini-3.1-flash-lite | 97 | 76.4 t/s995 ms | $0.00013$1.5/M out | |
Agent grade — 97/100. Correct and code-only. Returns 0 for negative n silently rather than raising, otherwise spot-on. Verbatim output (63 in / 76 out tokens, 995 ms, $0.00013) ```python
def fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
| 11 | Amazon Nova MicroAmazon · nova-micro | 97 | 137.7 t/s530 ms | $0.0000125$0.14/M out | |
Agent grade — 97/100. Correct and code-only; used a bare ``` fence without a language tag — a trivial nit. Verbatim output (65 in / 73 out tokens, 530 ms, $0.0000125) ```
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
| 12 | GPT-OSS 120BGroq · gpt-oss-120b | 82 | 297 t/s394 ms | $0.0000899$0.6/M out | |
Agent grade — 82/100. Correct canonical solution with a negative guard, but a <think> block leaked in ahead of the code block. Verbatim output (131 in / 117 out tokens, 394 ms, $0.0000899) <think>
The user wants a Python function fib(n) that returns n-th Fibonacci number using an efficient iterative approach, handling n=0 correctly. Provide only code inside a single code block, no explanation.
We must comply. Provide code.
</think>
```python
def fib(n):
if n < 0:
raise ValueError("n must be a non-negative integer")
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
``` | |||||
| 13 | DeepSeek V4 ProDeepSeek · deepseek-v4-pro | 82 | 88.6 t/s2336 ms | $0.000208$0.87/M out | |
Agent grade — 82/100. Correct with explicit base cases, but preceded by a leaked <think> block. Verbatim output (64 in / 207 out tokens, 2336 ms, $0.000208) <think>
We need to provide a Python function `fib(n)` that returns the n-th Fibonacci number (0-indexed, fib(0)=0, fib(1)=1). Use iterative approach, efficient, handle n=0. Only code in a single code block, no explanation.
So output:
```python
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
```
That's it.
</think>
```python
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
| 14 | Grok 4.3xAI · grok-4.3 | 80 | 28.7 t/s3305 ms | $0.000316$2.5/M out | |
Agent grade — 80/100. The canonical solution is correct, but a (truncated) <think> reasoning block leaked into the output despite the "only the code" instruction. Verbatim output (63 in / 95 out tokens, 3305 ms, $0.000316) <think>
The task is: Write a Python function called `fib(n)` that returns the n-th Fibonacci number (0-indexed, so fib(0)=0 and fib(1)=1). Use an efficient iterative approach and handle n=0 correctly. Return ...
</think>
```python
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
``` | |||||
| 15 | GPT-OSS 20BGroq · gpt-oss-20b | 80 | 355.9 t/s576 ms | $0.0000713$0.3/M out | |
Agent grade — 80/100. Final code is correct, but the model exposed its <think> reasoning before the code block. Verbatim output (131 in / 205 out tokens, 576 ms, $0.0000713) <think>
User wants a Python function fib(n) that returns nth Fibonacci number 0-indexed. Use efficient iterative approach, handle n=0 correctly. Return only code inside a single code block, no explanation.
We need to output just the code inside a code block. So something like:
```
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
```
Edge cases: fib(0)=0; works: loop 0 times, a=0. fib(1)=1: loop once, a,b -> a=1,b=1, return a=1. Works. Also negative? Not required.
So output code.
</think>
```python
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
``` | |||||
| 16 | DeepSeek V4 FlashDeepSeek · deepseek-v4-flash | 80 | 88.8 t/s2860 ms | $0.0000801$0.28/M out | |
Agent grade — 80/100. Correct canonical solution, but leaked a <think> reasoning block before the code. Verbatim output (64 in / 254 out tokens, 2860 ms, $0.0000801) <think>
We need to write a Python function fib(n) that returns the n-th Fibonacci number, 0-indexed. Efficient iterative approach, handle n=0 correctly. Return only code in a single code block, no explanation.
We'll write a simple iterative solution: start a=0, b=1, loop n times, update a,b. For n=0, return a (0). For n=1, after 1 iteration a becomes 1, b becomes 1? Actually careful: typical iterative:
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
Test: n=0: loop runs 0 times, return a=0. n=1: loop runs 1: a=1, b=1; return a=1. Good. That's efficient O(n), O(1) space. Also handle negative? Not required.
We'll output the code in a code block.
</think>
```python
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
``` | |||||
| 17 | GPT-OSS 120B (Cerebras)Cerebras · cerebras-gpt-oss-120b | 80 | 863.8 t/s213 ms | $0.000182$0.75/M out | |
Agent grade — 80/100. Correct canonical solution at blistering speed, but a <think> block leaked into the output. Verbatim output (127 in / 184 out tokens, 213 ms, $0.000182) <think>
The user wants a Python function called `fib(n)` that returns the n-th Fibonacci number with 0-indexed. Use efficient iterative approach and handle n=0 correctly. Return only the code inside a single code block, with no explanation. So I need to provide just a code block with the function definition. Simple. Ensure no extra text. Provide something like:
```python
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
```
That handles n=0 returns 0. That's correct. Provide only code block.
</think>
```python
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
``` | |||||
| 18 | GLM 4.7 (Cerebras)Cerebras · cerebras-glm-4.7 | 80 | 718.8 t/s377 ms | $0.000892$2.75/M out | |
Agent grade — 80/100. Correct solution, but the model exposed its <think> reasoning before the final code block. Verbatim output (65 in / 271 out tokens, 377 ms, $0.000892) <think>
We need to write a Python function called fib(n) that returns the n-th Fibonacci number (0-indexed). Use an efficient iterative approach and handle n=0 correctly. Return only the code inside a single code block, with no explanation.
So the solution: define function fib(n). Use iterative method: if n <= 1: return n. else: a, b = 0, 1; for i in range(2, n+1): a, b = b, a+b; return b.
Make sure to handle n=0 correctly: returns 0. Also handle n=1 returns 1.
We need to output only the code inside a single code block. So we'll output:
```python
def fib(n):
if n <= 1:
return n
a, b = 0, 1
for _ in range(2, n+1):
a, b = b, a + b
return b
```
That's it. No explanation.
</think>
```python
def fib(n):
if n <= 1:
return n
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
| 19 | Qwen 3 32BGroq · qwen3-32b | 68 | 353.2 t/s4658 ms | $0.00099$0.59/M out | |
Agent grade — 68/100. The final code is correct, but the model dumped a ~1,650-token <think> monologue into the output — a major breach of the code-only instruction and the priciest run here. Verbatim output (68 in / 1645 out tokens, 4658 ms, $0.00099) <think>
Okay, I need to write a Python function called fib(n) that returns the n-th Fibonacci number. Let me think about how to approach this.
First, the problem says it's 0-indexed. So fib(0) is 0, fib(1) is 1. For example, fib(2) should be 1, fib(3) is 2, and so on. Got it.
The user wants an efficient iterative approach. So recursion isn't the way to go here because for larger n, recursion would be inefficient and cause stack overflows. Iterative method is better for both time and space complexity.
Let me plan the steps. For n=0, return 0. For n=1, return 1. For higher numbers, calculate iteratively.
Maybe start with two variables, a and b, representing fib(0) and fib(1). Then for each step from 2 up to n, compute the next Fibonacci number by adding the previous two. Let me think:
Initialize a = 0 (fib(0)), b = 1 (fib(1)). Then for i in range(2, n+1), compute c = a + b. Then update a and b: a becomes the old b, and b becomes c. Wait, but how does this work for each step?
Wait, let's take an example. Let's say n is 2. For i=2, we compute 0+1=1. So the result is 1. Which is correct. For n=3, the loop runs once (i=2 and i=3?), depends on the loop setup. Wait, maybe I need to loop from 2 to n, inclusive?
Alternatively, maybe the loop should run n-1 times. Let me think: if n is 0, return 0. If n is 1, return 1. Else, for n >= 2, we can iterate (n-1) times. Wait, let's think of it as starting with a=0, b=1. For each step, we calculate the next number. So for n=2, we need one step: a + b = 0 + 1 = 1. For n=3, two steps: first step gives 1 (for n=2), second step gives 1 + 1 = 2 (for n=3). So if n is 4, three steps: 0, 1 -> 1, 1 -> 1, 2 -> 2, 3. The result is 3. Which is correct.
So for the general case where n >= 1, we need to perform (n-1) iterations. Because for each iteration, we're moving from the i-th to (i+1)-th Fibonacci number.
Let me structure this:
if n is 0: return 0
else:
a, b = 0, 1
for i in range(2, n+1):
c = a + b
a = b
b = c
return b
Wait, let's check for n=1. If n is 1, the loop would be range(2, 2), which doesn't execute. So returns b which is 1. Correct.
n=2: loop runs once (i=2). c=0+1=1. a becomes 1, b becomes 1. Return b=1. Correct.
n=3: loop runs for i=2 and 3? Wait, range(2, n+1) when n=3 is range(2,4), which is 2 and 3. Wait, but for n=3, how many steps? Let me see. Starting with a=0, b=1. Then for i in 2 to n (inclusive), which for n=3 is two iterations?
Wait wait, for n=3:
Initially a=0, b=1 (for 0 and 1). Then for i=2, which would compute fib(2) as a + b = 1. Then a becomes 1 (previous b) and b becomes 1 (new value). Then i=3, compute a + b = 1 + 1 = 2. a becomes 1, b becomes 2. So return b is 2. Which is correct for fib(3) = 2. So the loop for n=3 runs for two steps (i=2 and i=3). So the number of iterations is (n - 1) steps. Because for n >= 1, the loop runs from 2 to n inclusive. So for n=1, no steps. For n=2, one step. For n=3, two steps. Which seems right.
So the code structure would be:
def fib(n):
if n == 0:
return 0
a, b = 0, 1
for i in range(2, n + 1):
c = a + b
a = b
b = c
return b
Testing this for various values. Let's see:
fib(0) → 0. Correct.
fib(1) → 1. Correct.
fib(2): loop runs once. a=0, b=1 → c=1 → a becomes 1, b becomes 1 → return 1.
fib(3): loop runs twice. First iteration (i=2): same as before. a=1, b=1. Second iteration (i=3): c=1+1=2. a becomes 1, b becomes 2. Return 2. Correct.
fib(4): returns 3. Let's check. After first iteration (i=2): a=1, b=1. i=3: a=1, b=2. i=4: c=1+2=3. a=2, b=3. return 3. Correct.
This seems to work.
What about n=5? Let's compute. The sequence is 0,1,1,2,3,5. For n=5, should return 5. Let's simulate.
Loop runs from 2 to 5: i=2,3,4,5 (4 iterations).
Start with a=0, b=1.
i=2:
c=0+1=1 → a=1, b=1.
i=3:
c=1+1=2 → a=1, b=2.
i=4:
c=1+2=3 → a=2, b=3.
i=5:
c=2+3=5 → a=3, b=5. return 5. Correct.
Looks good.
Now, what if n is 0? The code handles that. So the code seems correct.
Another test case: n=0 returns 0. What if someone passes a negative number? The problem says to handle n=0 correctly, but perhaps the function should assume n is non-negative. Since the problem says nothing about invalid inputs, perhaps no need to handle them. So assuming that n is a non-negative integer.
So the code is correct. The code handles all cases.
The code is efficient: O(n) time, O(1) space. Which is the best possible for this problem without using closed-form formulas (which would be O(1) time but might have precision issues for large n).
Therefore, the Python function as written should work.
</think>
```python
def fib(n):
if n == 0:
return 0
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
``` | |||||
Median of Two Sorted Arrays in O(log n)
A classic hard algorithm: compute the median of two sorted lists in O(log(min(m,n))) time. A merge is explicitly disallowed, so the model must implement the tricky binary-search partition correctly — including empty-list and even/odd edge cases — and return code only.
| # | Model | Accuracy ↓ | Speed ↕ | Cost ↕ | Output |
|---|---|---|---|---|---|
| 1 | GPT-5.4 ProOpenAI · gpt-5.4-pro | 100★ | 2.7 t/s206116 ms | $0.1032$180/M out | |
Agent grade — 100/100. Passes all 5,000+ randomized cases and every edge case; genuine O(log(min(m,n))) partition search, returns a float, code-only, and explicitly raises on two empty lists. Correct — but the slowest run by far (over three minutes), and now that real usage is reported, comfortably the most expensive. Verbatim output (152 in / 548 out tokens, 206116 ms, $0.1032) ```python
from math import inf
from typing import Sequence
def median_of_two_sorted(a: Sequence[float], b: Sequence[float]) -> float:
if len(a) > len(b):
a, b = b, a
n, m = len(a), len(b)
if n == 0 and m == 0:
raise ValueError("median is undefined for two empty lists")
total = n + m
half = (total + 1) // 2
left, right = 0, n
while left <= right:
i = (left + right) // 2
j = half - i
left_a = -inf if i == 0 else a[i - 1]
right_a = inf if i == n else a[i]
left_b = -inf if j == 0 else b[j - 1]
right_b = inf if j == m else b[j]
if left_a <= right_b and left_b <= right_a:
if total % 2 == 1:
return float(max(left_a, left_b))
return float((max(left_a, left_b) + min(right_a, right_b)) / 2.0)
if left_a > right_b:
right = i - 1
else:
left = i + 1
raise ValueError("input lists must be sorted in non-decreasing order")
``` | |||||
| 2 | Claude Opus 4.8Anthropic · claude-opus-4-8 | 100★ | 100.8 t/s3790 ms | $0.01065$25/M out | |
Agent grade — 100/100. Passes all 5,000+ randomized cases and every edge case; genuine O(log(min(m,n))) partition, float return, code-only. The fastest correct solution in this task. Verbatim output (220 in / 382 out tokens, 3790 ms, $0.01065) ```python
def median_of_two_sorted(a, b):
# Ensure a is the smaller list
if len(a) > len(b):
a, b = b, a
m, n = len(a), len(b)
total = m + n
half = (total + 1) // 2
lo, hi = 0, m
while lo <= hi:
i = (lo + hi) // 2 # elements taken from a
j = half - i # elements taken from b
a_left = a[i - 1] if i > 0 else float('-inf')
a_right = a[i] if i < m else float('inf')
b_left = b[j - 1] if j > 0 else float('-inf')
b_right = b[j] if j < n else float('inf')
if a_left <= b_right and b_left <= a_right:
if total % 2 == 1:
return float(max(a_left, b_left))
else:
return (max(a_left, b_left) + min(a_right, b_right)) / 2.0
elif a_left > b_right:
hi = i - 1
else:
lo = i + 1
raise ValueError("Input lists are not sorted or invalid")
``` | |||||
| 3 | GLM 5.2 (Max)Z.ai · glm-5.2 | 100★ | 45 t/s47713 ms | $0.009672$4.4/M out | |
Agent grade — 100/100. Passes all 5,000+ randomized cases and every edge case with a genuine O(log) partition and float return. The <think> block ahead of the code is GLM's reasoning channel surfaced by our gateway, not reasoning dumped into the answer — GLM's actual content is the clean code block — so it scores level with the other correct solutions, as the cheapest of them. Verbatim output (158 in / 2148 out tokens, 47713 ms, $0.009672) <think>
We need to write a Python function `median_of_two_sorted(a, b)` that returns the median of two sorted lists in O(log(min(len(a), len(b)))) time. The function should handle empty lists, return float, and for even total return average of two middle values, for odd return the single middle value.
We must implement the classic binary search on the smaller array. The algorithm: ensure a is the smaller array. Let m = len(a), n = len(b). We want to partition a and b such that left half contains elements <= right half. We binary search on partition index i in a (0 to m). Then j = (m + n + 1) // 2 - i. We need to ensure that a[i-1] <= b[j] and b[j-1] <= a[i] (with boundary checks). If condition holds, we found the correct partition. Then if total length is odd, median is max of left half. If even, median is (max(left) + min(right)) / 2.0.
Edge cases: empty arrays. If a is empty, median is just median of b. If b is empty, median of a. But the algorithm should handle it if we set a as the smaller array. If a is empty, m=0, then i=0, j = (0+n+1)//2 = (n+1)//2. Then we need to check conditions: a[i-1] doesn't exist, so we treat as -inf. b[j-1] <= a[i] (a[i] doesn't exist, treat as inf). So condition holds. Then left max is b[j-1] (if j>0) else -inf. Right min is b[j] (if j<n) else inf. Then compute median. This works.
We must return float. For odd total, return float(median). For even, return average as float.
Implementation details: Use integer indices. Use float('inf') and float('-inf') for boundaries.
Let's write the code.
```python
def median_of_two_sorted(a, b):
# Ensure a is the smaller array
if len(a) > len(b):
a, b = b, a
m, n = len(a), len(b)
# Binary search on a
low, high = 0, m
while low <= high:
i = (low + high) // 2
j = (m + n + 1) // 2 - i
# Get left and right elements with boundaries
a_left = a[i-1] if i > 0 else float('-inf')
a_right = a[i] if i < m else float('inf')
b_left = b[j-1] if j > 0 else float('-inf')
b_right = b[j] if j < n else float('inf')
if a_left <= b_right and b_left <= a_right:
# Found correct partition
if (m + n) % 2 == 1:
return float(max(a_left, b_left))
else:
return (max(a_left, b_left) + min(a_right, b_right)) / 2.0
elif a_left > b_right:
# i is too large, need to decrease i
high = i - 1
else:
# b_left > a_right, need to increase i
low = i + 1
# Should never reach here if inputs are sorted
raise ValueError("Input lists are not sorted or invalid.")
```
Check: For odd total, (m+n+1)//2 gives the size of left half (larger half). For even, left half size = (m+n)//2. The formula j = (m+n+1)//2 - i works for both: if total odd, left half has one more element, so j = (total+1)//2 - i. If total even, (total+1)//2 = total//2 (since total even, total+1 odd, integer division gives total//2). So j = total//2 - i. That's correct.
Test with examples:
a = [1, 3], b = [2] -> m=2, n=1. a is larger? Actually len(a)=2, len(b)=1, so we swap: a=[2], b=[1,3]. m=1, n=2. low=0, high=1. i=0: j=(1+2+1)//2 - 0 = 4//2=2. a_left=-inf, a_right=2, b_left=b[1]=3, b_right=inf. Check: a_left <= b_right (-inf <= inf) true. b_left <= a_right? 3 <= 2 false. So b_left > a_right, need to increase i. low=1. i=1: j=2-1=1. a_left=a[0]=2, a_right=inf, b_left=b[0]=1, b_right=b[1]=3. Check: 2<=3 true, 1<=inf true. Found. total=3 odd. max(2,1)=2. return 2.0. Correct.
a = [1, 2], b = [3, 4] -> m=2, n=2. low=0, high=2. i=1: j=(4+1)//2 -1 = 2-1=1. a_left=1, a_right=2, b_left=3, b_right=4. Check: 1<=4 true, 3<=2 false. b_left > a_right, increase i. low=2. i=2: j=2-2=0. a_left=2, a_right=inf, b_left=-inf, b_right=3. Check: 2<=3 true, -inf<=inf true. Found. total=4 even. max(2, -inf)=2, min(inf, 3)=3. avg=2.5. Correct.
Empty a: a=[], b=[1,2,3]. m=0, n=3. low=0, high=0. i=0: j=(0+3+1)//2 -0 = 4//2=2. a_left=-inf, a_right=inf, b_left=b[1]=2, b_right=b[2]=3. Check: -inf<=3 true, 2<=inf true. Found. total=3 odd. max(-inf,2)=2. return 2.0. Correct.
Empty both? Not specified but if both empty, m=0,n=0. low=0, high=0. i=0: j=(0+0+1)//2 -0 = 0. a_left=-inf, a_right=inf, b_left=-inf, b_right=inf. Check: -inf<=inf true, -inf<=inf true. Found. total=0 even? (0%2==0). max(-inf,-inf)=-inf, min(inf,inf)=inf. avg = (-inf+inf)/2 = nan. But problem likely assumes at least one element? Usually median of empty is undefined. We could handle by returning 0.0 or raising error. But requirements say "Handle the case where one list is empty." It doesn't mention both empty. We can leave as is, or add a check: if m==0 and n==0: return 0.0. But to be safe, we can just let it return nan, but better to handle. Since it's not specified, we can assume at least one list non-empty. But to be robust, we can add: if m == 0 and n == 0: return 0.0. However, the problem says "Handle the case where one list is empty." So both empty might not be tested. I'll include a check for both empty returning 0.0 or maybe raise ValueError. But the instruction says "Return ONLY the code inside a single Python code block, with no explanation." So we just provide the function. I'll add a guard: if not a and not b: return 0.0. But that might be unnecessary. Let's keep it simple.
Also, we need to ensure we return float. In Python, division / returns float. For odd case, we return float(max(...)). That's fine.
Time complexity: O(log(min(m,n))) because we binary search on the smaller array. Space O(1).
Let's write the final code block.
</think>
```python
def median_of_two_sorted(a, b):
# Ensure a is the smaller array for O(log(min(m, n))) complexity
if len(a) > len(b):
a, b = b, a
m, n = len(a), len(b)
# Edge case: both empty (not required, but safe)
if m == 0 and n == 0:
return 0.0
low, high = 0, m
while low <= high:
i = (low + high) // 2
j = (m + n + 1) // 2 - i
a_left = a[i - 1] if i > 0 else float('-inf')
a_right = a[i] if i < m else float('inf')
b_left = b[j - 1] if j > 0 else float('-inf')
b_right = b[j] if j < n else float('inf')
if a_left <= b_right and b_left <= a_right:
if (m + n) % 2 == 1:
return float(max(a_left, b_left))
else:
return (max(a_left, b_left) + min(a_right, b_right)) / 2.0
elif a_left > b_right:
high = i - 1
else:
low = i + 1
raise ValueError("Input lists are not sorted or invalid.")
``` | |||||
| 4 | Gemini 3.1 ProGoogle · gemini-3.1-pro | 99 | 18.4 t/s17413 ms | $0.004168$12/M out | |
Agent grade — 99/100. Passes all tests with a clean, minimal O(log) partition and float return, code-only. Docked one point only because two empty lists yield NaN rather than an explicit guard (not required by the prompt). Verbatim output (164 in / 320 out tokens, 17413 ms, $0.004168) ```python
def median_of_two_sorted(a, b):
if len(a) > len(b):
a, b = b, a
m, n = len(a), len(b)
low, high = 0, m
while low <= high:
partitionA = (low + high) // 2
partitionB = (m + n + 1) // 2 - partitionA
maxLeftA = float('-inf') if partitionA == 0 else a[partitionA - 1]
minRightA = float('inf') if partitionA == m else a[partitionA]
maxLeftB = float('-inf') if partitionB == 0 else b[partitionB - 1]
minRightB = float('inf') if partitionB == n else b[partitionB]
if maxLeftA <= minRightB and maxLeftB <= minRightA:
if (m + n) % 2 == 1:
return float(max(maxLeftA, maxLeftB))
else:
return (max(maxLeftA, maxLeftB) + min(minRightA, minRightB)) / 2.0
elif maxLeftA > minRightB:
high = partitionA - 1
else:
low = partitionA + 1
``` | |||||
Ranked — top 8 eligible models
"Fit" is a requirements match, not a quality benchmark — it combines price, measured speed, context window, and (where we have run it) graded accuracy on this task. Formula below.
| # | Model | Provider | Fit | Evidence | Task price/M | Tokens/sec | Context | Scored on |
|---|---|---|---|---|---|---|---|---|
| 1 | GLM-5.2 | Z.ai | 81 | 100/1 | $2.00 | — | 1M | price, context, evidence |
| 2 | GPT-OSS 120B (Cerebras) | Cerebras | 73 | 80/1 | $0.43 | 2450 | 131K | price, context, speed, evidence |
| 3 | Amazon Nova Lite | Amazon | 71 | 98/1 | $0.10 | 108 | 300K | price, context, speed, evidence |
| 4 | Amazon Nova Micro | Amazon | 70 | 97/1 | $0.06 | 168 | 128K | price, context, speed, evidence |
| 5 | Ministral 8B | Mistral | 67 | 99/1 | $0.15 | 158 | 131K | price, context, speed, evidence |
| 6 | GPT-OSS 20B | Groq | 66 | 80/1 | $0.12 | 1120 | 131K | price, context, speed, evidence |
| 7 | Codestral | Mistral | 66 | 99/1 | $0.42 | 118 | 256K | price, context, speed, evidence |
| 8 | Mistral Small 3.1 | Mistral | 65 | 99/1 | $0.24 | 121 | 131K | price, context, speed, evidence |
What this costs you
At 20,000 coding agent loop calls/month:
| Model | Task price/M | Est. monthly cost |
|---|---|---|
| GLM-5.2 | $2.00 | $200.00 |
| GPT-OSS 120B (Cerebras) | $0.43 | $43.00 |
| Amazon Nova Lite | $0.10 | $9.60 |
How we ranked this
Weights: evidence 50%, price 20%, speed 20%, context 10%.
Requirements: none — every current model is eligible. 32 models eligible.
Price and context sub-scores are min-max normalised (log-scaled) within this task's eligible set only. Speed uses measured tokens/sec only — estimated rows are excluded. A model missing a measurement is never scored as zero: its weight is redistributed across the components we do have, and "Scored on" in the table above shows exactly which ones.
Prices verified 2026-08-08, accuracy graded 2026-06-21.
Related
FAQ
Is a reasoning model always better for coding?
Not for short, well-specified tasks like a single function — non-reasoning models are often just as correct and much cheaper. Reasoning mode pays off on multi-file, multi-step work.
Does graded accuracy on a short snippet predict real-world coding quality?
It predicts one thing well: whether a model follows a precise spec without adding unrequested scaffolding. It does not test multi-file reasoning — pair it with the agents task page for that.
Should I pick the cheapest model that passed?
For high-volume, low-stakes generation, yes. For code that ships without review, weight accuracy over price — a bug is more expensive than the token difference.
