Introduction

Edge Cases

Derive small counterexamples from a contract: empty inputs, endpoints, duplicates, missing values, and repeated state changes.

Free~11 min

What are edge cases?

Edge cases are inputs or operation sequences that cross a decision boundary in the contract. An empty list changes whether index zero exists. An exact timestamp changes whether an entry has expired. A repeated deletion changes whether the key is still present.

Choose cases by enumerating those states, then write the expected result before running the code. Start with the smallest input that distinguishes two branches. This makes a failure explainable and keeps the test connected to a requirement.

Respect the allowed inputs. If a task guarantees strictly increasing timestamps, out-of-order writes belong to a different contract. Concentrate on allowed states that your implementation could confuse.

Core operations

Contract featureCases to compareQuestion to answer
Collection sizeEmpty, singleton, first multi-item caseWhich access or loop becomes possible?
EndpointBefore, equal, afterWhich side owns equality?
IdentitySame value, different value, different indexWhat makes two entries distinct?
PresenceAbsent, present with zero/false/empty valueDoes the check test membership or truthiness?
State changeCreate, overwrite, delete, repeatWhich state should survive each operation?

Key patterns

Empty and minimal inputs

Separate an absent collection from a collection that contains an empty value.

When to use - serialization, first-element access, recursive base cases, and any loop that assumes at least one item.

How it works - identify the operation that first needs an element. Check the zero-element state before that operation, then test one element to exercise the shortest ordinary path. Preserve distinctions that the output contract requires.

Example: Count characters without losing item count. An empty list contains zero strings; [""] contains one string with zero characters. Both have a character total of zero, so that total cannot encode which input was supplied. Encode and Decode Strings Level 1 asks you to preserve the full distinction.

This example counts Unicode code points, which can differ from the number of visible characters when combining marks or joined emoji are present.

Note
Keep the expected distinction visible - the shape above differs for an empty list and one empty string even though their character totals match.

Endpoints and equality

Write down which branch should handle equality before choosing a comparison operator.

When to use - timestamps, interval overlap, insertion positions, binary search and expiration checks.

How it works - test the two sides and the endpoint itself. Under half-open interval semantics, a booking occupies its start but not its end. A later booking starting at that end can reuse the same resource.

Example: Reuse a room at its end time. A meeting occupies [4, 9). A new start of 8 still overlaps; starts of 9 and 10 can reuse the room. The equality case decides whether the condition should include the endpoint.

Note
Derive equality from the contract - an inclusive endpoint would require a different comparison; the operator follows the interval definition.

Duplicates and missing values

Test whether an entry exists independently of the value stored there.

When to use - counters, caches, optional values, duplicate detection and lookups with a default.

How it works - enumerate absent and present states separately. Inside the present state, include values that conditional expressions treat as false. If the contract allows a missing-value sentinel to be stored, membership is the only way to preserve the distinction.

Example: Read a stored count. The store has "ready": 0; querying "ready" must return zero, while querying the absent "waiting" uses the fallback 6. A truthiness-based fallback would incorrectly return 6 for both. Try these states in the collections map section.

Use key in counts for membership. counts.get(key, fallback) is also suitable for this integer-valued dictionary; counts.get(key) or fallback would replace a stored zero.

Note
Separate identity from equality - two equal values at different indices can be distinct items; a pair-sum solution must not reuse one index twice.

State transitions

Test a short sequence that moves the object into and out of the state your operation expects.

When to use - stores, caches, counters, expiration, undo operations and repeatable deletion.

How it works - write the state before and after each operation. Include an operation that changes an existing entry and one that targets an absent entry. Then check that unrelated entries survive.

Example: Delete a key twice. Start with "red": 3 and "blue": 8. Deleting "red" returns true and leaves "blue"; deleting "red" again returns false. An operation sequence exposes the missing-key behavior that a successful first deletion cannot check.

Note
Check what remains - returning the right deletion result is only part of the contract; the other key must still hold its original value.

When to reach for edge-case analysis

  • The code reads an element before checking whether it exists.
  • A comparison controls expiration, overlap or a search boundary.
  • A lookup combines membership and fallback behavior.
  • Equal values may still represent distinct items or positions.
  • Several methods operate on the same retained state.
  • A large failing example needs to be reduced to the first incorrect decision.

Done reading? Mark it so it sticks in your dashboard.

Discussion