lowkey
← All guides

How to find the bug in your LeetCode solution

Updated November 30, 2026 · 7 min read

You wrote a solution you're pretty sure is right. It passes the examples. Then you submit and get Wrong Answer on test 47 of 60, or a Time Limit Exceeded, or a runtime error on some input you can barely read. The worst move here is changing random things and resubmitting. This guide is a calmer process that finds the bug faster, and it's the same process that works in an interview where you can't submit at all.

First, figure out what kind of failure it is

Each failure type points at a different set of suspects:

  • Wrong Answer: a logic bug, a missed edge case, or a wrong idea. Start with the failing input.
  • Runtime Error: usually an index out of range, a null or None access, division by zero, or recursion that's too deep.
  • Time Limit Exceeded: either the approach is too slow for the constraints, or there's an accidental extra loop (like list.remove or x in list inside a loop, which are both linear).
  • Passes the examples but fails hidden tests: almost always an edge case or an overflow.

Shrink the failing input

If LeetCode shows you the failing input, copy it into the custom test box. If it's huge, try to find a small input that fails the same way. Cut it in half and see if it still fails. Keep the half that fails. A failing input with five elements is worth more than one with five thousand, because you can trace five elements by hand.

If you don't have a failing input (for example, in an interview), make your own. Go through the edge case list below and run each one in your head.

Trace by hand, line by line

Write down your variables as column headers and step through the code with your small input, updating each value the way the code actually would, not the way you meant it to. This is slow and a little boring, and it finds the bug almost every time. The bug is the first line where the value you wrote down is different from the value you expected.

Adding print statements is the fast version of this. Print the key variables at the top of each loop iteration, run the small input, and compare against what you expected. Remove the prints before you submit, because heavy printing can cause a time limit failure on its own.

The edge case checklist

Run through these for almost any array or string problem:

  • Empty input, and input of length one.
  • All elements the same.
  • Already sorted, and sorted in reverse.
  • Negative numbers and zero, if they're allowed.
  • Duplicates, especially when the problem says "unique" about the answer, not the input.
  • The answer is at the very start or the very end.
  • No valid answer exists. What should you return?
  • The largest values allowed. Does anything overflow a 32-bit int in Java or C++?

For linked lists, add: a single node, two nodes, and changing the head. For trees: an empty tree, a single node, and a tree that's really a straight line (which also tests recursion depth). For graphs: a disconnected graph and a graph with a cycle.

The off-by-one checklist

Off-by-one bugs cause more Wrong Answers than anything else. Check each of these on purpose:

  • Loop bounds: should it be < n or <= n? Does i + 1 go past the end on the last iteration?
  • Binary search: is your range inclusive [lo, hi] or half-open [lo, hi)? The loop condition and the updates must agree. Mixing them causes infinite loops or skipped elements.
  • Window length: a window from left to right inclusive has length right - left + 1, not right - left.
  • Prefix sums: if prefix[0] = 0, then the sum of nums[i..j] is prefix[j + 1] - prefix[i].
  • DP tables: is the table size n or n + 1, and does your base case match that choice?
  • Updating the answer: do you update it before or after you move the pointer? Try both on paper.

A worked example

Here's a binary search for the first index where nums[i] >= target. It hangs on some inputs.

def lower_bound(nums, target):
    lo, hi = 0, len(nums)
    while lo < hi:
        mid = (lo + hi) // 2
        if nums[mid] < target:
            lo = mid
        else:
            hi = mid
    return lo

Shrink and trace

Try nums = [1, 3] and target = 3. Start with lo = 0, hi = 2. Then mid = 1, nums[1] is 3, which isn't less than 3, so hi = 1. Now lo = 0, hi = 1, so mid = 0. nums[0] is 1, which is less than 3, so lo = 0. Nothing changed, so it loops forever.

The fix

If nums[mid] is less than the target, mid itself can't be the answer, so the new range should start after it: lo = mid + 1. With a half-open range, every branch has to make the range strictly smaller, and lo = mid doesn't when mid equals lo. That's the whole bug, and tracing a two-element input found it in under a minute.

When it's not a bug, it's the idea

Sometimes every line does what you meant, and the result is still wrong. That means the approach itself is wrong. A greedy choice that isn't actually safe is the classic case. To test this, try to build a small counterexample where your approach makes a choice that looks good now and hurts later. If you find one, stop patching and rethink. The unsticking routine is a good place to restart from.

A habit worth building: before you hit Submit, spend one minute running the edge case checklist in your head. In an interview, saying "let me check the empty case and a single element" out loud is a good sign to the interviewer, and it catches real bugs.