Input/Output Patterns & Edge Cases
š 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:
- What if the input is empty?
- What if there's only one element?
- What if all elements are the same?
- What if the input is already in the desired state (already sorted)?
- What about negative numbers?
- 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
1// EDGE CASE HANDLING ā Production-quality code23// BAD: Doesn't handle edge cases4function 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}1112// GOOD: Handles all edge cases13function findMax(arr: number[]): number | null {14 if (!arr || arr.length === 0) return null; // Empty/null check15 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}2122// BINARY SEARCH ā Edge cases matter23function binarySearch(arr: number[], target: number): number {24 if (arr.length === 0) return -1; // Edge: empty array2526 let lo = 0, hi = arr.length - 1;27 while (lo <= hi) {28 const mid = lo + Math.floor((hi - lo) / 2); // Edge: overflow prevention29 if (arr[mid] === target) return mid;30 if (arr[mid] < target) lo = mid + 1;31 else hi = mid - 1;32 }33 return -1;34}3536// PALINDROME ā Edge cases37function isPalindrome(s: string): boolean {38 if (s.length <= 1) return true; // Edge: empty or single char39 const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g, ''); // Edge: case, specials40 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}4748// TEST YOUR EDGE CASES49function testEdgeCases() {50 // Array edge cases51 console.log(findMax([])); // null52 console.log(findMax([5])); // 553 console.log(findMax([3,3,3])); // 354 console.log(findMax([-5,-1,-10])); // -15556 // String edge cases57 console.log(isPalindrome("")); // true58 console.log(isPalindrome("a")); // true59 console.log(isPalindrome("A man, a plan, a canal: Panama")); // true6061 // Number edge cases62 console.log(Number.MAX_SAFE_INTEGER); // 900719925474099163 console.log(0.1 + 0.2 === 0.3); // false!64 console.log(Number.isNaN(NaN)); // true65}
šļø Practice Exercise
Practice Problems:
- List 5 edge cases for "reverse a linked list"
- Write a function that handles ALL edge cases for "find two numbers that sum to target"
- What edge cases would you test for a "valid parentheses" checker?
- Given a binary search, what happens with: empty array, one element, target not present, duplicates?
- Write edge case tests for: "find the longest common prefix of an array of strings"
- What's the edge case that makes naive quicksort O(n²)?
- List edge cases for a function that checks if a binary tree is balanced
- Write a function that safely divides two numbers (handle 0, NaN, Infinity)
- What are the edge cases for "merge two sorted arrays"?
- 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