KMP and Rabin-Karp for GATE: The Failure Function and Rolling Hash, Fully Solved
Build the LPS array without guessing, trace a KMP fallback that never moves the text index back, and calculate every Rabin-Karp window hash by hand.
KnowledgeGate Team
Exam prep & CS education

String matching questions are usually won or lost in a small preprocessing table. If one KMP fallback is wrong, every later alignment can look plausible while the answer drifts away.
The cure is to define LPS precisely and compute it one comparison at a time. Rabin-Karp then supplies the second exam pattern: a rolling hash that is fast only because every hash hit is checked against the actual characters.
1. The setup: naive matching and its cost
Let text T have length n and pattern P have length m. The task is to find every starting index where the complete pattern occurs in the text.
Naive matching aligns P at a text position and compares characters. On a mismatch, it shifts the pattern by one and starts the comparison again. Previously checked text characters may therefore be checked repeatedly. The worst-case running time is O(n x m).
KMP prevents that repeated text work by using information about the pattern itself. Rabin-Karp computes a compact hash for each length-m text window and performs character comparison only when the window hash matches the pattern hash.
The algorithms save work in different ways:
KMP: use prefix and suffix structure to choose a safe shift.
Rabin-Karp: use a rolling hash to reject most windows cheaply.
2. The KMP failure function, or LPS array
For each pattern position i, lps[i] is the length of the longest proper prefix of P[0..i] that is also a suffix of P[0..i]. Proper means the prefix cannot be the whole substring.
Build the array from left to right. Maintain len, the length of the current candidate prefix, and start with lps[0] = 0.
If
P[i] == P[len], increaselen, store it inlps[i], and move to the nexti.If the characters differ and
len > 0, setlen = lps[len - 1]and compare again at the samei.If they differ and
len == 0, setlps[i] = 0and move on.
The fallback does not guess a smaller length. It follows a previously computed LPS value. During matching, the same table tells KMP how much of the pattern prefix is already known to agree with the text suffix.
3. Fully worked example: LPS of ababaca
Take P = a b a b a c a, with indices 0 through 6.
| Prefix ending at | Comparison and fallback |
|
|---|---|---|---|
0 |
| Proper prefix cannot equal the whole string | 0 |
1 |
|
| 0 |
2 |
|
| 1 |
3 |
|
| 2 |
4 |
|
| 3 |
5 |
|
| 0 |
6 |
|
| 1 |
Therefore:
lps = [0, 0, 1, 2, 3, 0, 1]
Check the difficult position directly. For ababac, possible non-empty proper prefixes are a, ab, aba, abab and ababa. None is also a suffix because the substring ends in c. Therefore lps[5] must be 0, which independently confirms the fallback calculation.
![The LPS table for the pattern ababaca, with the fallback at index 5 stepping from len 3 to lps[2] = 1 and then to lps[0] = 0.](https://cdn.knowledgegate.ai/blog-assets/blog_asset_1784089455599_2cxpot.jpg)
4. KMP matching run: find ababaca in abababacaba
Now use:
T = a b a b a b a c a b a, indices 0 through 10P = a b a b a c a, lengthm = 7lps = [0, 0, 1, 2, 3, 0, 1]
Let i index the text and j index the pattern.
T[0..4] = ababamatchesP[0..4] = ababa. The indices are nowi = 5,j = 5.Compare
T[5] = bwithP[5] = c. They differ.Since
jis not zero, setj = lps[j - 1] = lps[4] = 3. Keepi = 5.Compare
T[5] = bwithP[3] = b. Match, soi = 6,j = 4.T[6] = amatchesP[4] = a, givingi = 7,j = 5.T[7] = cmatchesP[5] = c, givingi = 8,j = 6.T[8] = amatchesP[6] = a, givingi = 9,j = 7.
Now j = m, so a full match has ended. Its starting index is:
i - m = 9 - 7 = 2
Direct verification gives T[2..8] = ababaca, exactly the pattern. The text index never moved backwards. The LPS value preserved the already useful suffix aba and avoided checking it again from the start.
![KMP matching abababacaba: the mismatch at T[5], the lps = 3 shift that puts P[3] under T[5], and the full match starting at index 2.](https://cdn.knowledgegate.ai/blog-assets/blog_asset_1784089456069_ywvgs6.jpg)
5. Rabin-Karp: rolling hash and spurious hits
Rabin-Karp represents each length-m window in base d modulo a prime q. It compares the window hash with the pattern hash. Equal hashes make a window a candidate, not a confirmed match, because different strings can have the same remainder.
For a numeric example, take:
pattern
P = "26"text
T = "31415926535"base
d = 10modulus
q = 11
The pattern hash is 26 mod 11 = 4. The text has ten windows of length 2:
Start index | Window | Value mod 11 | Result |
|---|---|---|---|
0 |
| 9 | Hash differs |
1 |
| 3 | Hash differs |
2 |
| 8 | Hash differs |
3 |
| 4 | Spurious hit, |
4 |
| 4 | Spurious hit, |
5 |
| 4 | Spurious hit, |
6 |
| 4 | Verified match |
7 |
| 10 | Hash differs |
8 |
| 9 | Hash differs |
9 |
| 2 | Hash differs |
There are exactly three spurious hits and one real match at index 6.
The rolling update is:
new = (d x (old - leading x d^(m - 1)) + incoming) mod q
For the first raw window value, move from 31 to 14:
Remove the leading digit:
31 - 3 x 10 = 1.Shift the remaining digit left:
1 x 10 = 10.Add the incoming digit:
10 + 4 = 14.Reduce modulo 11:
14 mod 11 = 3.
The same idea updates each later window in constant time. Always normalise a negative modular result if the implementation language can return a negative remainder.
6. Complexity and how GATE tests this
KMP spends O(m) time building LPS and O(n) time scanning the text, for O(n + m) total time. Its control flow can fall back within the pattern, but the text index never backs up.
Rabin-Karp also preprocesses the pattern and scans the windows efficiently on average, giving O(n + m) average time in the standard analysis. Its worst case is O(n x m) when many windows share the hash and each candidate requires a full character check.
Typical questions ask you to compute an LPS array, trace a KMP shift, count spurious Rabin-Karp hits, apply one rolling update, or choose the correct complexity. Watch for two traps: omitting the word proper from the LPS definition, and treating every hash equality as a string match.
Our practice bank has over 1,100 questions on algorithms, covering string matching, hashing and complexity. Confirm the current Algorithms scope and any weightage detail on the official GATE portal for the relevant cycle.
7. The short version and your next step
KMP uses the LPS array to reuse a matched prefix without moving the text index back. Rabin-Karp rolls a hash to reject windows quickly, then verifies the characters because collisions create spurious hits. KMP is linear in the combined input size; Rabin-Karp is linear on average but can fall to O(n x m).
Connect the prefix-table habit to dynamic programming explained and the collision idea to hashing and collision resolution. Then build topic coverage with GATE Guidance by Sanchit Sir, practise under time pressure in the GATE Test Series, and use the GATE category for the wider path.
Keep learning

ISRO Scientist/Engineer CS Syllabus vs GATE CS: The Subject Overlap Map
See which core CS subjects transfer directly from GATE to ISRO, what drops out, how the question texture changes and where an ISRO-specific polish helps.

Union-Find for GATE: Union by Rank, Path Compression and the Near-Constant Bound
Trace seven unions, inspect the final ranks, then compress the deepest path by hand. The worked forest makes the inverse-Ackermann bound much less mysterious.

RAID Levels for GATE: Striping, Mirroring, Parity and the Two Capacity Formulas
Stop memorising RAID names in isolation. Tie each level to usable capacity, failure tolerance and write cost, then test the formulas on one eight-disk array.

Memory Interfacing for GATE: Chip-Count and Address-Decoding Numericals, Fully Worked
Separate words from bits, split the address correctly, and verify the result with a gap-free hexadecimal map. This guide works through depth and width expansion step by step.