Edge Cases Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Edge Cases.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

Edge cases are unusual or boundary inputs that sit at the limits of what a program is expected to handle. They often reveal bugs that do not appear in ordinary examples.

If normal inputs show whether the code works, edge cases show whether it is truly robust.

Read the full concept explanation โ†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Boundary inputs are where hidden assumptions tend to break.

Common stuck point: An edge case is not the same as an invalid case. Some edge cases are valid inputs that still need the correct output.

Sense of Study hint: After testing a normal case, immediately ask: What is the smallest valid input? The largest? What if there is nothing there? What if two values are equal?

Common Mistakes to Watch For

Before you work through the examples, skim the mistake guide so you know which shortcuts and sign errors to avoid.

Worked Examples

Example 1

medium
Trace `binary_search(arr, target)` on `arr = [5]` with `target = 5`. List the boundary edge cases you should also test.

Answer

Index 00 found; edge cases: empty array, target smaller than all, target larger than all, target equal to first/last.

First step

1
Single-element array: low=0, high=0, mid=0; arr[0]==target so return 0.

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Unlock answer keys One Family plan โ€” every worked solution, all subjects

Example 2

medium
A `sum_positive(nums)` function should sum only positive numbers in a list. Identify three edge-case inputs whose behavior must be explicitly tested.

Example 3

medium
Trace `index_of(arr, x)` returning the first index of xx or โˆ’1-1. What edge cases warrant tests?

Example 4

hard
A `merge(a, b)` merges two sorted lists. Argue why testing on `a = []` plus a generic `b`, and on two equal-length lists with interleaved values, covers most boundary bugs.

Example 5

hard
Quicksort with first-element pivot: name two adversarial inputs that turn its expected O(nlogโกn)O(n \log n) into worst-case O(n2)O(n^2).

Example 6

hard
A graph BFS function expects no self-loops. What three structural edge cases should you test even so?

Example 7

medium
A counter is incremented by two concurrent threads. What edge case makes a simple `counter += 1` lose updates?

Example 8

challenge
Given `int mid = (low + high) / 2;` in C with 32-bit ints, name the edge case that produces a wrong `mid` for very large arrays, and the standard fix.

Example 9

hard
A regex `^\d+$` should match strings of one-or-more digits. What edge inputs reveal whether the implementation is multi-line aware?

Example 10

medium
A `between(x, lo, hi)` is intended INCLUSIVE. List inputs to test inclusive vs exclusive behavior at boundaries.

Example 11

medium
A `min()` function on integers compares to a starting value of 00. Why is this wrong?

Example 12

medium
A pagination function shows items 1..pageSize1..\text{pageSize} for page 1. What boundary value of total items reveals an empty last page?

Example 13

hard
Identify the COMPLETE boundary inputs to test for `inRange(x, lo, hi)`: list five distinct test classes.

Example 14

hard
A login system rejects passwords >64> 64 chars. What attacker-relevant edge case might still slip in?

Example 15

hard
A binary search uses `mid = (lo + hi) / 2`. For very large `lo`, `hi`, this is an edge case. State a fix.

Example 16

challenge
A timezone-aware function bookkeeps daylight saving transitions. Which two clock-time edge cases occur on a DST 'spring forward' day?

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

easy
A function computes the average of a list of numbers. Which input is the classic edge case that can crash it?

Example 2

easy
For a function that processes a non-negative integer count, which value is the smallest boundary you should test?

Example 3

easy
A list-indexing function accepts indices 0 to n-1. Which index is the upper-boundary edge case?

Example 4

easy
A function takes a 32-bit signed integer. Which extreme value is a key edge case?

Example 5

easy
A search function should handle a target that is NOT in the list. Why is this an important edge case?

Example 6

easy
A negative number is passed to a `factorial(n)` function defined only for nโ‰ฅ0n \ge 0. What category of input is this?

Example 7

easy
A string-reversal function is given the empty string "". What should a robust function return?

Example 8

easy
A function divides two integers. Which divisor value is the must-test edge case?

Example 9

medium
A function returns the first element of a list: `return list[0]`. List the edge case(s) that break it and the minimal fix.

Example 10

medium
A loop runs `for i in 1..n` and accesses `arr[i]` on a 0-indexed array of length n. What edge-related bug exists, and at which index does it manifest?

Example 11

medium
A `clamp(x, lo, hi)` should keep x within [lo, hi]. Which three test inputs for x best probe the boundaries when lo=0, hi=10?

Example 12

medium
A function sums positive numbers but uses a signed counter that overflows at 231โˆ’12^{31}-1. With inputs that push the running total past this, what edge-case failure occurs?

Example 13

medium
A date function must handle Feb 29. In which case is Feb 29 a VALID input rather than invalid?

Example 14

medium
A binary search assumes the list is sorted. What 'edge' input class โ€” beyond empty/single โ€” most often breaks it and gives wrong answers silently?

Example 15

medium
A program reads user input expecting a number but the user types letters. What is this kind of edge case, and what is the robust response?

Example 16

medium
A function `maxOf(list)` is correct for lists of size โ‰ฅ2\ge 2 but you never tested size 1. Why is the single-element list a meaningful edge case?

Example 17

medium
A function reads the LAST element with `list[len(list)-1]`. Which input makes this index negative or invalid, and what is the failure?

Example 18

challenge
You must enumerate the COMPLETE set of boundary inputs to test a function `clamp(x, lo, hi)` for correctness of its comparisons. List the distinct relative positions of x that fully cover the boundaries.

Example 19

challenge
A recursive function `sum(list)` is `if empty return 0 else return list[0] + sum(rest)`. Identify the base-case edge input and explain why omitting it causes a specific failure.

Example 20

challenge
A function processes the range [a,b][a, b] inclusive with `for i in a..=b`. For which relationship between a and b does the 'empty range' edge case appear, and what should the function do?

Example 21

easy
A function `max(a, b)` is implemented as `if a > b: return a else: return b`. What does it return when a=ba = b?

Example 22

easy
A string function uses `s[0]` to peek at the first character. Which input is the obvious edge case?

Example 23

easy
A function accepts a 32-bit signed integer. What two boundary values should you test?

Example 24

medium
A pseudocode function `factorial(n)` returns 11 if n=0n=0 else nโ‹…factorial(nโˆ’1)n \cdot \text{factorial}(n-1). Why is n=0n=0 an edge case rather than a 'normal' input?

Example 25

medium
A web form accepts a username 1โ€“20 characters long. List 4 distinct edge-case lengths you should test.

Example 26

medium
A loop is `while low <= high`. If low=high\text{low} = \text{high}, how many iterations run (assuming the body doesn't change them)?

Example 27

medium
A function `is_palindrome(s)` checks if a string reads the same forwards and backwards. What does it return for `s = ""` (empty) and `s = "a"` (single char)?

Example 28

medium
Floating-point: which two non-finite values should a numerical function explicitly handle as edge cases?

Example 29

hard
A function that splits a string on commas treats `",a,b,"` as input. What four edge-case results does the implementation need to choose between?

Example 30

hard
A hash table doubles its bucket count when load factor exceeds 0.750.75. What edge case occurs precisely at the resize boundary?

Example 31

hard
A binary tree traversal uses recursion. What is the input edge case that causes a stack overflow in many runtimes, even for a 'small' tree by count?

Example 32

hard
A function `clamp(x, lo, hi)` returns xx clamped to [lo,hi][\text{lo}, \text{hi}]. Which precondition is a hidden edge case the docs should call out?

Example 33

medium
A REST endpoint accepts an optional query parameter `limit`. What three edge values must its handler explicitly handle?

Example 34

medium
A function reads lines from a file. List two file-existence edge cases and two file-content edge cases worth testing.

Example 35

challenge
Property-based testing generates many inputs automatically. What philosophy of edge-case discovery does it embody compared to hand-written cases?

Example 36

challenge
A cache uses an LRU policy with capacity 11. What edge cases collapse the cache's behavior to something simpler?

Example 37

easy
A function returns the length of a list. What is the result for the empty list?

Example 38

easy
What edge case might break a function `divide(a, b)` returning a/ba/b?

Example 39

easy
For a function accepting ages 00 to 120120, what are the two boundary test values?

Example 40

easy
For a function expecting a date in the range Jan 1, 2000 to Dec 31, 2099, name one BOUNDARY date to test.

Example 41

medium
A function returns the median of a list. Which list size and content is a key edge case?

Example 42

medium
A loop `for i in 0..n` accesses `arr[i+1]`. At what index does it overflow?

Example 43

medium
A function computes (a+b)/2(a+b)/2 as a midpoint. For 32-bit signed ints with a=b=230a=b=2^{30}, what edge case occurs?

Example 44

medium
A function expects user-supplied JSON. Name three edge cases at the input boundary.

Example 45

medium
A string-trim function removes leading/trailing whitespace. What edge case is `" "` (only spaces)?

Example 46

medium
A function returns `arr[len(arr) / 2]` as median. For even length, what is wrong?

Example 47

hard
A floating-point comparison `if x == 0.1 + 0.2` fails. Why is this an edge case?

Example 48

hard
A recursive `factorial(n)` uses base `if n == 1`. What edge input breaks it?

Example 49

hard
A counter increments daily and stored as `int8` (max 127). After how many days does it overflow?

Example 50

challenge
A file upload accepts up to 100 MB. Name three edge cases at the size boundary.

Example 51

challenge
A binary heap remove-min uses array index 11 as root with children at 2i,2i+12i, 2i+1. For a heap of size nn, what edge case occurs at index nn when nn is even?

Background Knowledge

These ideas may be useful before you work through the harder examples.

testing