Input/Output Patterns & Edge Cases

0/12 in this phase0/57 across the roadmap

šŸ“– Concept

The difference between passing and failing a coding interview often comes down to edge cases. Handling them shows maturity and production-level thinking.

Common edge cases by data type:

Arrays:

  • Empty array []
  • Single element [5]
  • All identical elements [3,3,3,3]
  • Already sorted / reverse sorted
  • Contains negative numbers
  • Contains duplicates
  • Very large array (n = 10⁶)

Strings:

  • Empty string ""
  • Single character "a"
  • All same characters "aaaa"
  • Spaces, special characters, unicode
  • Palindromes
  • Case sensitivity

Numbers:

  • Zero, negative, very large
  • Integer overflow
  • Floating point precision
  • MIN_SAFE_INTEGER / MAX_SAFE_INTEGER

Trees/Graphs:

  • Null/empty tree
  • Single node
  • Skewed tree (like a linked list)
  • Disconnected graph
  • Cycles in graph
  • Self-loops

General patterns to always check:

  1. What if the input is empty?
  2. What if there's only one element?
  3. What if all elements are the same?
  4. What if the input is already in the desired state (already sorted)?
  5. What about negative numbers?
  6. What about very large inputs?

šŸ  Real-world analogy: Edge cases are like testing a door — you don't just check if it opens normally. You check: locked, unlocked, kicked, pulled instead of pushed, opened during an earthquake. The "normal" case is easy; the edges reveal quality.

šŸ’» Code Example

codeTap to expand ā›¶
1// EDGE CASE HANDLING — Production-quality code
2
3// BAD: Doesn't handle edge cases
4function findMaxBad(arr) {
5 let max = arr[0]; // šŸ’„ Crashes on empty array!
6 for (let i = 1; i < arr.length; i++) {
7 if (arr[i] > max) max = arr[i];
8 }
9 return max;
10}
11
12// GOOD: Handles all edge cases
13function findMax(arr: number[]): number | null {
14 if (!arr || arr.length === 0) return null; // Empty/null check
15 let max = arr[0];
16 for (let i = 1; i < arr.length; i++) {
17 if (arr[i] > max) max = arr[i];
18 }
19 return max;
20}
21
22// BINARY SEARCH — Edge cases matter
23function binarySearch(arr: number[], target: number): number {
24 if (arr.length === 0) return -1; // Edge: empty array
25
26 let lo = 0, hi = arr.length - 1;
27 while (lo <= hi) {
28 const mid = lo + Math.floor((hi - lo) / 2); // Edge: overflow prevention
29 if (arr[mid] === target) return mid;
30 if (arr[mid] < target) lo = mid + 1;
31 else hi = mid - 1;
32 }
33 return -1;
34}
35
36// PALINDROME — Edge cases
37function isPalindrome(s: string): boolean {
38 if (s.length <= 1) return true; // Edge: empty or single char
39 const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g, ''); // Edge: case, specials
40 let l = 0, r = cleaned.length - 1;
41 while (l < r) {
42 if (cleaned[l] !== cleaned[r]) return false;
43 l++; r--;
44 }
45 return true;
46}
47
48// TEST YOUR EDGE CASES
49function testEdgeCases() {
50 // Array edge cases
51 console.log(findMax([])); // null
52 console.log(findMax([5])); // 5
53 console.log(findMax([3,3,3])); // 3
54 console.log(findMax([-5,-1,-10])); // -1
55
56 // String edge cases
57 console.log(isPalindrome("")); // true
58 console.log(isPalindrome("a")); // true
59 console.log(isPalindrome("A man, a plan, a canal: Panama")); // true
60
61 // Number edge cases
62 console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
63 console.log(0.1 + 0.2 === 0.3); // false!
64 console.log(Number.isNaN(NaN)); // true
65}

šŸ‹ļø Practice Exercise

Practice Problems:

  1. List 5 edge cases for "reverse a linked list"
  2. Write a function that handles ALL edge cases for "find two numbers that sum to target"
  3. What edge cases would you test for a "valid parentheses" checker?
  4. Given a binary search, what happens with: empty array, one element, target not present, duplicates?
  5. Write edge case tests for: "find the longest common prefix of an array of strings"
  6. What's the edge case that makes naive quicksort O(n²)?
  7. List edge cases for a function that checks if a binary tree is balanced
  8. Write a function that safely divides two numbers (handle 0, NaN, Infinity)
  9. What are the edge cases for "merge two sorted arrays"?
  10. Create a testing template that covers: empty, single, duplicates, negative, large input

āš ļø Common Mistakes

  • Not checking for empty/null input — the #1 runtime error in interviews

  • Assuming all numbers are positive — many problems have negative numbers that change the approach

  • Not testing with the smallest valid input (n=0, n=1) — most bugs appear at boundaries

  • Forgetting that strings can contain spaces, special characters, or be case-sensitive

  • Not considering integer overflow — (lo + hi) / 2 can overflow in many languages

šŸ’¼ Interview Questions

šŸŽ¤ Mock Interview

Practice a live interview for Input/Output Patterns & Edge Cases