String Algorithms and Pattern Matching Questions
Advanced string processing beyond basic manipulation: substring search (KMP, Rabin-Karp, Z-algorithm), tries and suffix structures, edit distance, and text-parsing problems. Covers the algorithmic machinery behind search, autocomplete, and tokenization. Distinct from introductory string manipulation in depth and complexity.
Implement a rolling-hash based substring search (Rabin-Karp) in Java that finds all occurrences of a fixed-length pattern in a text. Use base=256 and mod=1000000007. Show how to update the rolling hash in O(1) when sliding the window and describe how to handle and detect hash collisions.
Write a Python function z_search(text: str, pattern: str) -> List[int] that finds all occurrences of pattern using the Z algorithm by computing Z on pattern + '$' + text. Implement the Z-array computation in O(n) time and return starting positions in text where pattern matches.
Implement the Knuth-Morris-Pratt (KMP) substring search algorithm. Given text and pattern strings, implement computation of the lps (longest proper prefix which is also suffix) table and use it to search efficiently in O(n+m) time. Explain how lps avoids re-comparison.
Explain the Z-algorithm for string matching and Z-array computation. Given the string 'aabcaabxaaz', compute the Z array manually and explain how you would use the Z algorithm to search for a pattern 'aabx' inside a larger text using the 'pattern$text' trick.
Given string s, design an algorithm to count the number of distinct substrings of s. Outline two methods: using suffix array with LCP and using suffix automaton. Explain time/space complexity of each approach, how to handle large alphabets, and when one approach is preferable over the other for memory or performance constraints.
Unlock Full Question Bank
Get access to all 47 String Algorithms and Pattern Matching interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.