# Searching & String Pattern Matching (Animated): Binary, Interpolation, Rabin-Karp & KMP
Exam: GATE Computer Science & Information Technology (GATE CS)
Subject: Algorithms
Canonical URL: https://gateandnet.com/exams/gate-cs/algorithms/searching-algorithms-and-kmp-pattern-matching-animated

> 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`:

```visualizer
binary-search
```

## 2. Binary Search Variants & Specialized Array Searches

1. **First Occurrence (`lower_bound`):** Even when `A[mid] == target`, record `ans = mid` and set `high = mid - 1` to keep searching left!
2. **Search in Rotated Sorted Array (`[4, 5, 6, 7, 0, 1, 2]`):** At least **one** of the two halves `A[low..mid]` or `A[mid..high]` is **guaranteed strictly sorted** — check whether `target` lies inside the sorted half in $\mathcal{O}(1)$ and halve in $\mathcal{O}(\log n)$!
3. **Interpolation Search:** Probes position $pos = low + \left\lfloor \frac{(target - A[low])(high - low)}{A[high] - A[low]} \right\rfloor$. Achieves **$\mathcal{O}(\log \log n)$** on uniformly distributed keys, degrading to $\mathcal{O}(n)$ on exponential distributions like $[1, 2, 4, 8, \dots, 2^n]$.

## 3. String Pattern Matching: Naive vs Rabin-Karp vs KMP (Animated)

| Algorithm | Preprocessing | Matching Time (Avg / Worst) | Core Mechanism |
| :--- | :---: | :---: | :--- |
| **Naive Sliding Window** | $0$ | $\mathcal{O}(nm)$ worst (e.g. $T=a^n, P=a^{m-1}b$) | Resets $j=0$, backs up $i$ |
| **Rabin-Karp** | $\Theta(m)$ | Avg $\mathcal{O}(n+m)$, Worst $\mathcal{O}(nm)$ | Rolling Horner hash $\bmod q$; **spurious hits** on collision! |
| **Knuth-Morris-Pratt (KMP)** | $\Theta(m)$ | **$\Theta(n + m)$ Worst-Case!** | `LPS[]` failure table; $i$ **never** decrements |
| **Boyer-Moore** | $\Theta(m + |\Sigma|)$ | Best **$\mathcal{O}(n / m)$** sublinear! | Right-to-left scan + Bad-Character & Good-Suffix jumps |

```visualizer
kmp
```

## Solved Practice Questions & PYQs

### [GATE CS 2022 | MCQ]
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"`?
**Correct Answer:** A
**Solution:** - `"A"` $\to 0$
- `"AB"` $\to 0$
- `"ABA"` $\to 1$ (`"A"`)
- `"ABAB"` $\to 2$ (`"AB"`)
- `"ABABA"` $\to 3$ (`"ABA"`)
- `"ABABAC"` $\to 0$
- `"ABABACA"` $\to 1$ (`"A"`)
Result: **`[0, 0, 1, 2, 3, 0, 1]`**.

### [GATE CS 2018 | NAT]
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 **$1,000$** elements?
**Correct Answer:** 10
**Solution:** Worst-case probes $= \lfloor \log_2(1000) \rfloor + 1 = 9 + 1 = \mathbf{10}$ (since $2^9 = 512 \le 1000 < 1024 = 2^{10}$).
