A girl and a robot thinking together

Think together · First, a short story

A robot passes the two sample cases in a problem, but fails on another input with the same score. Samples are a few windows—not the whole house.

Lay out the process

Follow the numbered steps, then explain them in your own words.
  1. Pass the samplesCheck some cases
  2. Add the smallest inputCheck boundaries
  3. Add duplicates and extremesCheck assumptions
  4. Use the maximum sizeCheck efficiency

Passing the samples only shows that the answers for those inputs are correct; it does not prove correctness for every valid input.

You must also check whether the algorithm fully covers the problem, and whether the value types, boundaries, and complexity are appropriate.

01What exactly are sample cases?

Samples are a small set of input/output pairs provided by the problem author to help explain the task and check a basic implementation.

They are not the complete test data.

02Why can samples “fool” an incorrect program?

A bug may appear only on certain special inputs.

For example, a maximum-finding program might initialize its answer to zero:

int maximum = 0;

It passes samples with positive numbers, but fails when every input value is negative.

03Common cause 1: missed boundary cases

  • n = 1
  • All values are equal
  • The target does not exist
  • The last position
  • An empty set, if allowed

04Common cause 2: mishandled value ranges

The logic can be right and still get WA because an int overflows.

long long sum = 0;

Choose a wider type by estimating the largest possible result.

05Common cause 3: the algorithm is too slow

A small sample may contain only 10 values, so O(n²) runs quickly. The full test may contain 200,000 values, making the same solution time out.

06Common cause 4: incorrect output format

Extra debug text or a missing space may not always matter (that depends on the judge), but the required number of lines, contents, and order must match the specification exactly.

07How can you create counterexamples?

Smallest

n = 1

Largest

Near the constraint limit

Extreme order

Increasing / decreasing

Special values

Zero, negatives, duplicates

What have you learned?

  • Passing samples cannot prove a program is fully correct.
  • Hidden tests cover many more valid inputs.
  • Boundaries, overflow, complexity, and output format can all cause failure.
  • Create counterexamples instead of relying only on the official samples.
  • Correctness comes from reasoning that covers every valid input.

Next: How can you find bugs in your own program?

Your turn to try

A sorting program is tested only on 1, 2, 3. Which important examples might be missing?

Ready? Open the explanation

Reverse order, duplicate values, and a single element. Already-sorted input may hide comparison or swapping bugs.