INTERMEDIATEFrequently Tested 4 min read 5 views
Searching & String Pattern Matching (Animated): Binary, Interpolation, Rabin-Karp & KMP
Overflow-safe Binary Search loop invariants, Rotated Array & Ternary Search, O(log log n) Interpolation Search, Rabin-Karp rolling modular hash & spurious hits, and animated KMP LPS matching.
1. Interactive Binary Search Interval-Halving Visualizer
Step through both Present (47, 12, 94) and Absent (50) keys to observe how low becomes the exact sorted insertion index (lower_bound) when low > high:
Interactive Step-by-Step Animation
Binary Search (Interval Halving)
Worst O(⌊log₂ n⌋ + 1)Space: O(1) iterativeInvariant: target ∈ [L..H]
Target Key: 47Probes Used: 0 / 4
[0]
7
L[1]
12
[2]
19
[3]
24
[4]
31
[5]
38
[6]
47
[7]
56
[8]
63
[9]
72
[10]
85
[11]
94
HSearch for target = 47 in sorted array of 12 keys (max ⌊log₂(12)⌋ + 1 = 4 probes).
1 / 7
2. Binary Search Variants & Specialized Array Searches
- First Occurrence (
lower_bound): Even whenA[mid] == target, recordans = midand sethigh = mid - 1to keep searching left! - Search in Rotated Sorted Array (
[4, 5, 6, 7, 0, 1, 2]): At least one of the two halvesA[low..mid]orA[mid..high]is guaranteed strictly sorted — check whethertargetlies inside the sorted half in and halve in ! - Interpolation Search: Probes position . Achieves on uniformly distributed keys, degrading to on exponential distributions like .
3. String Pattern Matching: Naive vs Rabin-Karp vs KMP (Animated)
| Algorithm | Preprocessing | Matching Time (Avg / Worst) | Core Mechanism |
|---|---|---|---|
| Naive Sliding Window | worst (e.g. ) | Resets , backs up | |
| Rabin-Karp | Avg , Worst | Rolling Horner hash ; spurious hits on collision! | |
| Knuth-Morris-Pratt (KMP) | Worst-Case! | LPS[] failure table; never decrements | |
| Boyer-Moore | $\Theta(m + | \Sigma | )$ |
Interactive Step-by-Step Animation
KMP String Matching (LPS Table)
Θ(n + m) GuaranteedSpace: O(m) LPS arrayText pointer i NEVER backs up!
Text Pointer
i = 0 (Monotonic!) • Pattern Pointer j = 0Character Comparisons: 0A
B
A
B
D
A
B
A
C
D
A
B
A
B
C
A
B
A
B
A
0B
0A
1B
2C
0A
1B
2A
3B
4Precomputed LPS (Longest Proper Prefix also Suffix) table for "ABABCABAB": [0, 0, 1, 2, 0, 1, 2, 3, 4].
1 / 25
Key Formulas Summary
Binary vs Interpolation vs Jump vs Exponential Search Complexity
Note: Always write mid = low + (high - low) / 2 to avoid signed 32-bit integer overflow in (low + high)!
KMP Prefix Function LPS[q] & Total Amortized Time
Note: j increments at most n times and decreases by >= 1 per LPS fallback => <= 2n total loop steps!
#Algorithms#Binary Search#Interpolation Search#KMP#Rabin-Karp#String Matching#GATE CS
Practice Questions on This Chapter (2)
Q1GATE CS 2022MCQMEDIUM
+2M • -0.67
In the Knuth-Morris-Pratt (KMP) string matching algorithm, what is the LPS[] (Longest Proper Prefix which is also a Suffix) array for the pattern P = "ABABACA"?
⚡ Click an option above to verify instantly
Q2GATE CS 2018NATEASY
+1M • 0 Neg
What is the maximum number of key comparisons made by standard iterative Binary Search (A[mid] == x, <, > counted as 1 three-way probe per iteration) when searching in a sorted array of elements?