Diagnosing LeetCode Submissions That Pass Run Code but Fail Submit
Run Code passing while Submit fails usually means hidden edge cases, state leaks, or complexity issues. Here is a diagnostic guide.
21 Nov 2025, 22:08 UTC

The problem: Run Code passes, Submit fails
Your solution returns the expected output for LeetCode's sample inputs, but Submit returns Wrong Answer, Time Limit Exceeded, or Runtime Error. The useful takeaway is that Run Code is evidence only for the inputs you exercised. Submit is the authoritative evaluation and may cover edge cases, larger inputs, and repeated invocations that your local run never touched.
Recognizable condition
The implementation matches the examples or your custom tests, but the submission verdict is negative on the full test suite. Treat any displayed failing case as a clue, not a complete diagnosis.
Cause and diagnostic table
| Symptom on Submit | Likely cause | First diagnostic |
|---|---|---|
| Wrong Answer on hidden cases | Missed edge case, incorrect assumption, input mutation, reused state | Inspect any exposed failing input against a simple reference implementation |
| Time Limit Exceeded | Excessive complexity or repeated work | Derive growth from the stated constraints, not from sample runtime |
| Runtime Error | Invalid index, null handling, recursion depth, allocation | Minimize the input and locate the failing operation |
| Varying verdicts across similar cases | Shared or incompletely initialized state | Isolate state so each invocation starts clean |
Ordered checks
- Record the verdict. Note the language, exact verdict, and any failing input shown by the judge.
- Reproduce unchanged. Run that exact input through Run Code or a local harness before changing anything.
- Check boundary shapes. Test minimum size, duplicates, sorted and reverse-sorted data, extreme values, and disconnected components where applicable.
- Confirm the contract. Verify preprocessing, return type, and whether the problem permits mutating the input.
- Reset per-call state. Make all state local to the method or explicitly reset it; do not assume a fresh object or process for each test case.
- Derive complexity. Calculate time and auxiliary-space growth and compare it with the constraints. Auxiliary space is the extra memory your algorithm uses beyond the input itself.
- Differential-test correctness. For small valid inputs, compare your implementation against a deliberately simple brute-force reference.
Fixes tied to findings
- Edge-case mismatch. Correct the invariant or base case. Avoid adding a special case for only the displayed input.
- State contamination. Move state into local variables or reset it at the start of each call. LeetCode may reuse a single
Solutionobject across many test cases. - Unintended mutation. Copy or restore input data when the problem allows it, especially if you sort or modify arrays in place.
- Time limit exceeded. Remove the asymptotically dominant loop or recursion. Local timing is not directly comparable with judge timing; use it to study growth, not to predict an exact margin.
- Runtime error. Fix the identified bounds check, null guard, recursion depth, or allocation issue.
Re-run the original reproducer after each change, then Submit. A passing local run does not establish full-suite correctness.
Concrete example: reused state in a Python class
Consider a graph traversal problem where the solution tracks visited nodes in an instance attribute:
class Solution:
def __init__(self):
self.seen = set()
def can_visit_all_rooms(self, rooms):
# BUG: self.seen is not cleared between calls.
# If the judge reuses this object, earlier test cases pollute later ones.
self._dfs(rooms, 0)
return len(self.seen) == len(rooms)
def _dfs(self, rooms, node):
self.seen.add(node)
for key in rooms[node]:
if key not in self.seen:
self._dfs(rooms, key)
Run Code may pass because the harness often creates a fresh object for one call. Submit can fail when the judge calls the method repeatedly on the same instance. This is an illustrative pattern, not a tested submission. The fix is to make the state local or reset it explicitly:
class Solution:
def can_visit_all_rooms(self, rooms):
seen = set()
def dfs(node):
seen.add(node)
for key in rooms[node]:
if key not in seen:
dfs(key)
dfs(0)
return len(seen) == len(rooms)
Verify by reproducing the exact failing input locally before applying the change.
Local reproduction command
Run a local harness from your project directory. Replace solution.py with your file and [[input_payload]] with the exact failing input in the format expected by your test runner:
python3 -m pytest tests/test_solution.py -k "failing_case" -v
Where to run: your local project directory. Required permissions: read access to the solution and test files. Expected check: the local run reproduces the same wrong output or error. Risk: a local pass does not guarantee judge acceptance because the local environment may differ in recursion limits, memory, or input ordering.
Limitations and how to check the result
- A verdict alone does not identify a unique cause, and a displayed failing case may be unavailable or insufficient to reconstruct the full test set.
- Local runtime is not directly comparable with judge runtime. Use local runs to study growth and reproducibility, not to predict submission margins.
- Platform interface labels and exposed failing-case details can change; the diagnostic sequence does not depend on a particular page layout.
Practical check: on a practice problem, run the sample, then add a custom boundary case such as the minimum allowed input size. Submit and confirm that successful limited runs do not prove full-suite correctness.
Escalation criteria
Escalate beyond ordinary debugging only after all of the following are true:
- You have a minimal valid input that reproduces the verdict.
- You derived the expected result directly from the problem statement.
- The failure reproduces deterministically.
- You cross-checked with an independent reference where feasible.
If a contradiction remains, report the problem title, language, minimal code and input, your expected reasoning, and the actual result. Avoid prematurely claiming a judge defect.