Practice Discrete Math

Algorithms / Rolling Hash Substrings

Least You Need to Know: Rolling Hashes and Substring Fingerprints

A **rolling hash** gives a compact fingerprint for a substring so adjacent windows can be compared or updated quickly. Interview prompts use it for repeated-substring checks, duplicate-window detection, and fast candidate comparison.

The least you need to know

Key notation

H[i] prefix hash up to position i under a fixed convention
base multiplier used to weight character positions
mod modulus used to bound hash values

Tiny worked example

  • Suppose you hash every length-5 substring of a long string.
  • After computing the first window, the next window can remove the outgoing character contribution, multiply or shift as needed, and add the incoming character.
  • That reuses old work, which is why rolling hashes help avoid recomputing all 5 characters each time.
  • If two window hashes match, you may still verify the substrings directly when correctness matters.

Common mistakes

How to recognize this kind of problem

Start practice