Skip to content
UniCircle
EasyConceptualDebugging and testing

Your program gives the wrong answer on hidden tests. How do you debug it?

Question PROG-DBG-007

In an online coding round your solution passes the sample cases but fails some hidden tests with "Wrong Answer". Describe a systematic way to find the bug.

Show the explanation

Interviewers want to see a method, not guessing. A good order:

1. Re-read the problem statement. Many wrong answers come from misreading it.

  • Input and output format: separators, a trailing newline, upper or lower case, "print -1 if impossible".
  • Constraints: can values be negative, zero or equal? Can n be 0 or 1? Is the input guaranteed sorted, distinct or connected?
  • What exactly is asked: count or list, 0- or 1-indexed, modulo 10^9 + 7?

2. Check the usual suspects

  • Integer overflow: sums or products beyond 2^31. Use long long or long, and check intermediate values such as a * b before taking a modulus.
  • Off-by-one errors in loop bounds, binary search bounds and array sizes.
  • Edge cases: an empty input, one element, all elements equal, the maximum n, negative numbers.
  • State not reset between test cases (global arrays, static variables).
  • Floating-point comparisons and rounding. Use integers or an epsilon.
  • An uninitialised variable, or reading input in the wrong order.

3. Write your own tests

  • Hand-made edge cases with answers you know.
  • Stress testing, the most powerful technique: write a slow but obviously correct brute-force solution. Generate thousands of small random inputs, compare the two outputs automatically, and stop at the first mismatch. A small failing input usually makes the bug obvious.

4. Narrow it down

  • Print intermediate values (to stderr, so the output stays clean) or use a debugger with breakpoints.
  • Check your invariants: what should be true at each step of the loop?
  • Rubber-duck it: explain the code line by line, out loud or in comments.

5. Fix the cause, then re-test everything: the samples, your edge cases and the stress test, so the fix does not break something else.

Mention the other verdicts too: Time Limit Exceeded points to the algorithm's complexity or slow I/O (sys.stdin.readline in Python, ios::sync_with_stdio(false) in C++). Runtime Error points to out-of-bounds access, division by zero, recursion depth or a null pointer.

Original UniCircle question. Last reviewed 22 Oct 2026.