Resource · Guide or article
Rabin Karp Algorithm
Introduces Rabin-Karp, the hashing-based string search credited on the page to Karp and Rabin, and explains why hash matches still need a direct comparison.
Reviewed
Open the original: Rabin Karp Algorithm (opens the original site in a new tab)Practice this topicOpens the practice interviewer in Technical / coding mode; choose a question there.
Add to a roadmap
Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.
Source content: English
In computer science, the Rabin–Karp algorithm or Karp–Rabin algorithm is a string searching algorithm created by Richard M. Karp and Michael O. Rabin (1987) that uses hashing to find any one of a set of pattern strings in a text. The Rabin–Karp algorithm seeks to speed up the testing of equality of the pattern to the substrings in the text by using a hash function. A hash function is a function which converts every string into a numeric value, called its hash value; for example, we might have hash('hello') = 5. The algorithm exploits the fact that if two strings are equal, their hash values are also equal. Thus, string matching is reduced (almost) to computing the hash value of the search pattern and then looking for substrings of the input string with that hash value. However, there are two problems with this approach. First, because there are so many different strings and so few hash values, some differing strings will have the same hash value. If the hash values match, the pattern and the substring may not match; consequently, the potential match of search pattern and the substring must be confirmed by comparing them; that comparison can take a long time for long substrings…
PrepKit overview
PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.
- Suited to
- The source does not state a level
- Format
- Reference
What it covers
- Explains reducing pattern matching to comparing hash values, and how collisions force a confirmation step
- Notes that a rolling hash keeps successive substring hashes cheap, and says the example uses a polynomial hash rather than a Rabin fingerprint
- Gives average and best case O(n + m) time with O(p) space, and worst case O(n * m)
- Describes plagiarism detection as a multi-pattern use case
How to use it
PrepKit study advice, not a claim made by the source.
Implement a polynomial rolling hash, slide it across a text, and log every hash hit along with whether the direct comparison confirmed it. You should then be able to explain where the worst case comes from.
Scope and limitations
Conceptual only: no worked rolling-hash arithmetic, no code walkthrough and no exercises. Deeper detail is left to the Wikipedia and YouTube links.
Access and format
README on GitHub in trekhleb/javascript-algorithms (MIT). English, free to read.
Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88
Evidence behind this overview
- src/algorithms/string/rabin-karp/README.md
Pages that include this
Get the PrepKit newsletter
Optional. You don't need it to use PrepKit.