INTERVIEW MASTERY / ALGORITHM BRIEF

Edge-case analysis

Edge-case analysis is systematically testing the inputs where code usually breaks: empty and single-element inputs, duplicates, negative numbers, extreme values, and boundaries between cases.

BeginnerPhase 09 / Topic 3 of 6Mental modelComplexityEdge cases
01

Overview

Edge-case analysis is systematically testing the inputs where code usually breaks: empty and single-element inputs, duplicates, negative numbers, extreme values, and boundaries between cases. Most interview bugs are not in the main idea but at these edges.

Strong candidates list edge cases before coding (so the design handles them) and trace them after coding (to verify). A short, reusable checklist per input type turns this into a habit rather than guesswork.

Testing a bridge

Engineers do not only test a bridge with average traffic. They test it empty, with the heaviest truck, in strong wind, and at the joints where sections connect, because failures happen at the extremes and boundaries.

02

When to use it

  • During clarification, to agree on expected behavior.
  • Before coding, to shape the design (dummy nodes, sentinels, padding).
  • After coding, as a verification pass instead of saying 'I think it works'.
  • When a solution fails hidden tests.
03

Problem patterns it solves

Empty and single element

Recognize it when: array of length 0 or 1, empty string, null root.

  • 104. Maximum Depth of Binary Tree (null root)
  • 53. Maximum Subarray (single element)
All same / all negative

Recognize it when: duplicates everywhere, no positive values.

  • 53. Maximum Subarray (all negative)
  • 26. Remove Duplicates from Sorted Array
  • 169. Majority Element
Overflow and extremes

Recognize it when: values near 2^31 - 1, very large n.

  • 7. Reverse Integer
  • 8. String to Integer (atoi)
  • 29. Divide Two Integers
  • 69. Sqrt(x)
Boundaries between cases

Recognize it when: first and last index, even vs odd length, target at the ends.

  • 704. Binary Search
  • 876. Middle of the Linked List
  • 35. Search Insert Position
Disconnected or cyclic structure

Recognize it when: graphs with isolated nodes or cycles; linked lists with a cycle.

  • 207. Course Schedule
  • 141. Linked List Cycle
  • 547. Number of Provinces
04

Where it is used in real software

Production incidents

Many outages come from unexpected inputs: empty lists, null fields, time zones, leap years, or integer overflow (the 2038 problem).

Property-based testing

Tools like fast-check (JS) and jqwik (Java) generate random and extreme inputs automatically to find edge-case bugs.

Input validation

APIs validate boundaries (length limits, ranges) because edge inputs are also common attack vectors.

05

Key terms

Boundary value
An input at the edge of a valid range: 0, 1, max, max + 1.
Degenerate input
Empty, single element, all identical, or maximally skewed.
Off-by-one
A loop or index that runs one step too many or too few.
Sentinel
A dummy value or node that removes special cases.
06

Edge-case checklist by input type

  1. 1
    Arrays and strings

    Empty, length 1, length 2, all equal, sorted and reverse-sorted, duplicates, negatives and zero.

  2. 2
    Numbers

    0, 1, -1, the maximum and minimum integers, overflow when adding or multiplying.

  3. 3
    Linked lists and trees

    null head or root, single node, skewed tree, cycle, operation on the head or tail.

  4. 4
    Graphs

    No edges, disconnected components, self-loops, cycles, duplicate edges.

  5. 5
    Answers

    No valid answer, multiple valid answers, answer at the first or last position.

07

Edge cases for binary search (return index or -1)

Implementation: while (lo <= hi), mid = lo + ((hi - lo) >> 1)

Step 1 / 7
InputTargetExpectedWhat it tests
[]3-1empty array: loop must not run
[5]50single element found
[5]3-1single element missing
[1, 3, 5, 7]10target at the left boundary
[1, 3, 5, 7]73target at the right boundary
[1, 3, 5, 7]4-1target between elements
[1, 3, 5, 7]9-1target beyond the end

NOWInput: [] | Target: 3 | Expected: -1 | What it tests: empty array: loop must not run

Seven quick traces cover every branch of the loop. Tracing them takes about two minutes and catches almost every off-by-one error.

08

Implementation

// Small test harness you can type in an interviewfunction check(fn, cases) {  for (const [args, expected] of cases) {    const actual = fn(...args);    const ok = JSON.stringify(actual) === JSON.stringify(expected);    console.log(ok ? "PASS" : "FAIL", JSON.stringify(args), "->", actual, "expected", expected);  }} function maxSubArray(nums) {  let current = nums[0], best = nums[0]; // not 0: handles all-negative input  for (let i = 1; i < nums.length; i++) {    current = Math.max(nums[i], current + nums[i]);    best = Math.max(best, current);  }  return best;} check(maxSubArray, [  [[[5]], 5],                      // single element  [[[-3, -1, -2]], -1],            // all negative  [[[0, 0, 0]], 0],                // all zero  [[[-2, 1, -3, 4, -1, 2, 1]], 6], // typical  [[[2, -1, 2]], 3],               // best spans a negative]);
09

Complexity and performance

Time to spend2-5 minutes

Tracing 4 to 7 targeted cases.

Cases per problem4-7

Typical, empty, single, extremes, boundaries.

10

Trade-offs

Handling vs rejecting

Decide with the interviewer whether invalid input should throw, return a sentinel, or is impossible by contract.

Special cases vs uniform code

Sentinels and dummy nodes often remove special cases entirely, which is cleaner than many if statements.

11

Variants and related techniques

Randomized comparison testing

Compare an optimized function with a brute-force version on many random small inputs.

Boundary value analysis

Formal testing technique: test at, just below, and just above each boundary.

12

Common mistakes

  • Only testing the provided example.

    Fix: Examples are usually typical cases; add at least empty, single, and extreme inputs.

  • Initializing best with 0 for max problems.

    Fix: All-negative inputs break it; start from the first element or -Infinity.

  • Ignoring integer overflow in Java.

    Fix: Check before multiplying or use long.

13

Interview questions

What edge cases would you test for a linked list reversal?

An empty list, a single node, two nodes, and a longer list; also confirm the new head is returned and the old head's next is null.

How do you avoid special-casing the head of a linked list?

Use a dummy node before the head, perform operations uniformly, and return dummy.next.

14

Practice problems

ProblemDifficultyWhat it trains
7. Reverse IntegerMediumOverflow checks.
8. String to Integer (atoi)MediumWhitespace, signs, overflow.
69. Sqrt(x)Easy0, 1, and large x.
35. Search Insert PositionEasyBoundaries.
53. Maximum SubarrayMediumAll negative.