Skip to main content
Intermediatecoding Verified

Find all occurrences of a short pattern string inside a much longer text, more efficiently than checking every position character by character.

ReportedSource: GeeksforGeeks interview experience: Google SDE 2024

Quick answerFree

Rabin-Karp rolling hash (expected O(n+m), verify on hash match) or KMP with a prefix-function table (worst-case O(n+m)); naive is O(n·m).

Detailed answer· ~120 words

Interview Prep

Unlock the full answer with Interview Prep

Get a ~120-word model answer and a runnable reference solution, plus 28 more Algorithms questions.

Also in Pro (₹499 / 90 days).

Reference solution· runs in the browser sandbox

Interview Prep
Unlock with Interview Prep

+ 28 more Algorithms questions in Interview Prep, each with the full answer.

Discussion