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 function edit_distance(a: str, b: str) -> int that computes Levenshtein distance between two strings using dynamic programming. Discuss time/space complexity and how you would optimize space to O(min(m,n)) when only the distance (not alignment) is needed. Explain relevance in data cleaning for label normalization.
Implement Knuth–Morris–Pratt (KMP) substring search: kmp_search(text: str, pattern: str) -> List[int] returning start indices where pattern occurs. Explain how the failure function (lps) is computed and why the algorithm runs in linear time. Discuss memory and when KMP beats simpler algorithms.
Implement Rabin–Karp rolling hash substring search: rabin_karp(text, pattern) -> List[int]. Provide code for computing rolling hash and explain how false positives are handled and why average performance can be good. Discuss numerical issues (modulus, overflow) and tradeoffs vs KMP.
Longest Duplicate Substring: Given a string s, find the longest substring that appears at least twice (may overlap). Describe a method using binary search on length plus rolling hash to detect duplicates and implement the core checking routine in Python. Discuss collision handling and time/memory complexity.
That is every published String Algorithms and Pattern Matching question for AI Engineer so far. Browse the other topics in this category, or practice this one interactively.