Skip to content

Resource · Guide or article

Polynomial Rolling Hash

Builds from general hash functions up to the polynomial rolling hash behind Rabin-Karp string search. Good background if substring matching or string hashing questions come up and you want to know why the modulo is taken at every step.

Reviewed

Open the original: Polynomial Rolling Hash (opens the original site in a new tab)

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

Hash functions are used to map large data sets of elements of an arbitrary length (the keys) to smaller data sets of elements of a fixed length (the fingerprints). The basic application of hashing is efficient testing of equality of keys by comparing their fingerprints. A collision happens when two different keys have the same fingerprint. The way in which collisions are handled is crucial in most applications of hashing. Hashing is particularly useful in construction of efficient practical algorithms.

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
Article

What it covers

  • Fingerprints and collisions, plus the three inputs a rolling hash update needs: the old hash, the value leaving the window and the value entering it
  • The polynomial hash formula with constant p and modulus M, illustrated by turning the word ace into a base-26 key
  • Two JavaScript hash() functions: a naive power-based one and a Horner's method version that applies the modulo inside the loop to avoid overflow
  • Connects the rolling property to the Rabin-Karp pattern matching algorithm

How to use it

PrepKit study advice, not a claim made by the source.

Implement Rabin-Karp with the Horner-style hash, sliding a window across a text and confirming each candidate match with a direct comparison. Use a deliberately tiny modulus to force a collision so you can explain why that comparison is needed.

Scope and limitations

The snippets are JavaScript and show only a static hash, not the window update or a full Rabin-Karp search. Advice on choosing p and M is one sentence, and there is no complexity section.

Access and format

README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository; English, free to read, with inline JavaScript snippets.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88

Evidence behind this overview
  • src/algorithms/cryptography/polynomial-hash/README.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email