How to Find Edge Cases in Coding Problems Before You Submit

Blog › Exam Preparation

· by Julia Berens · 4 min read

A dark laptop displaying green and white code sits beside a beige coffee mug on a warm wooden desk against a charcoal backdrop.

Learning how to find edge cases in coding problems starts with reading the statement for limits and special rules. A sample input with five different numbers may show how the output is formatted, but it cannot tell you what your code does with one number, repeated values, or a tie. Instead of guessing which extra inputs to try, pick a few that each check a specific rule. Write down the correct output before you run your solution.

How to find edge cases in coding problems from constraints

Read the input constraints and the output instructions together. The constraints tell you what can happen. The output instructions tell you what your program must do when it happens. If the statement says a list has at least one element, test a one-element list rather than an empty one. If a value can be zero, check whether zero has any special meaning. If the statement says what to do when two answers are equally good, build an input that forces that choice.

Suppose a practice problem gives you a list of n integers, where n is 1 to 100,000 and each integer is 0 to 1,000,000,000. Repeats are allowed. Print the integer that occurs most often. If several integers share the highest count, print the smallest of them. The answer is the integer itself, not how many times it occurs.

Underline the lower and upper limits for the list length and the values. Circle “most often” and “smallest” to separate the primary rule from the tie rule. The smallest integer should win only when it shares the highest count. A good test list needs a case where it should win and another where it should lose.

Build a short list with one job per test

You do not need to try every combination of length, value, and order. Give each test a reason to stay. Start with a one-element list, then test what changes when values repeat. Use small lists so you can check the answers without relying on your program. In the table, each bracketed list represents the values; when you run your code, supply the length and use whatever format the problem requires.

ValuesExpected outputWhat the test checks
[0]0The shortest valid list and the lowest allowed value.
[6, 6, 6]6All values match. Print the value, not its count of 3.
[0, 9, 9]9The highest count beats the smallest value. The last entry makes the difference.
[8, 3, 8, 3, 1]3Repeated values tie. The first value is not the winner, and the smallest value overall is not eligible.
[1000000000, 4, 2]2All values tie at one occurrence. The upper value limit is valid.

Once you have a draft solution, add tests for assumptions your code makes. If you chose an array indexed by value, for example, the last row should make you check its size. A list with at most 100,000 entries can still contain the value 1,000,000,000. The maximum number of entries does not limit the size of an individual value.

Work out the expected output without your code

Use the statement as your source of truth, not a trace of your implementation. For [8, 3, 8, 3, 1], make a tally on paper: 8 appears twice, 3 appears twice, and 1 appears once. The highest count is 2, so only 8 and 3 qualify. Of those, 3 is smaller. Write 3 beside the input before you run anything.

It helps to write a one-line reason under each answer. For [0, 9, 9], that reason is “9 occurs twice; 0 occurs once.” If your program prints 0, check whether it applied the smallest-value rule too early or failed to count the final entry. If it prints 2, inspect what you send to output: your program may have found the correct count but printed it instead of the winning value.

Try reversing the tied list as a follow-up. The counts do not change, so the answer stays 3. That check is useful because a program that picks whichever tied value it sees first can pass one ordering and fail another. Keep the original and the reversed input if they help you catch that specific mistake.

Make a large test with an answer you can verify

A five-entry test can expose a wrong tie rule, but it cannot tell you much about how the program handles the longest valid list. Do not type 100,000 entries by hand. Generate a list containing 50,000 copies of 1,000,000,000 followed by 50,000 copies of 0. The two counts are equal by construction, so the expected output is 0. You can check the answer without reading the generated list item by item.

Change exactly one of the zeros to 1,000,000,000 and keep the length the same. Now the larger value occurs 50,001 times and zero occurs 49,999 times. The expected output changes to 1000000000. Together, these inputs check whether the program distinguishes a tie from a narrow win at the maximum length.

A run on your computer is useful for spotting slow code, but it does not guarantee the same timing elsewhere. Also check how the work grows with the input size. An approach that scans the whole list once for every entry can do far more work on a long list than the small tests suggest.

Keep a test that catches each bug you fix

When a test fails, first check that you entered it in the required format and that your expected output follows the statement. Then try to shrink the input while keeping the failure. If [8, 3, 8, 3, 1] fails, try [8, 3, 8, 3]. If both fail, the shorter list lets you focus on the tie between 8 and 3. If only the original fails, keep the 1: a smaller value outside the tie may be part of the bug.

After a fix, rerun the failing case and the rest of your short list. A change that makes the smaller value win a tie should not make [0, 9, 9] return zero. For your next practice session, pick a problem from the ICSC training and practice problems and write three tests with expected outputs before coding. The Exam Preparation articles offer more ways to practice checking your work.

Frequently asked questions

Should I test an empty list?
Test an empty list if the problem allows one. If the minimum length is one, use a one-element list to check the lower boundary.

What if the problem allows more than one correct output?
Write down what makes an output valid rather than requiring one exact answer. For example, if any most frequent value is allowed, check that the returned value has the highest count.

How many edge cases should I test?
There is no fixed number. Start with one test for each distinct limit or special rule, and add a case when your code makes an assumption the existing list does not check.

About the International Computer Science Competition: an online computer science competition for high school students from around the world. How the rounds work

Written by Julia Berens, Social Media Assistant of the ICSC team.

More articles

All articles