Challenge your knowledge of classical string search algorithms with focused questions on the KMP algorithm, Rabin-Karp hashing, and the Z-algorithm. This quiz helps solidify your understanding of key concepts, practical techniques, and subtle distinctions involved in efficient string pattern search methods.
Which of the following best describes the main purpose of the failure (prefix) function used in the Knuth-Morris-Pratt (KMP) algorithm during pattern searching?
Explanation: The failure function in KMP helps determine where to resume the pattern search in case of a mismatch, avoiding unnecessary re-checks. It does not calculate hashes (which is a Rabin-Karp concept), nor does it use rolling hashes to find matches. Preprocessing the text is not required by KMP—the function only preprocesses the pattern.
In the Rabin-Karp algorithm, what is the key risk associated with using hash values to compare substrings in the text to the pattern?
Explanation: Hash collisions in Rabin-Karp mean two different substrings may produce the same hash value, necessitating direct string comparison after hash matches to avoid false positives. The algorithm typically has average linear time, not quadratic. The pattern can be shorter or equal to the text in regular use cases, and any sequence, including numbers, can be searched if encoded correctly.
Given the string 'aabcaabxaaaz' and its Z-array, what does a value of Z[i] = 3 at some position i indicate in the context of the Z-algorithm?
Explanation: A Z[i] value of 3 means that the substring starting at position i matches the prefix of the string for 3 consecutive characters. It does not indicate the number of patterns, nor does it count mismatches, or relate directly to the pattern length unless the prefix itself is exactly that long.
Which statement accurately compares the worst-case time complexity of searching for a pattern of length m in a text of length n using the KMP, Rabin-Karp, and Z-algorithm?
Explanation: Both KMP and the Z-algorithm achieve linear time O(n + m) for pattern matching, while Rabin-Karp can have O(nm) complexity if many hash collisions occur. Rabin-Karp's best average performance is linear but not always. KMP is not quadratic, and the Z-algorithm is indeed used for pattern matching.
Which part of the input does the Z-algorithm preprocess before searching for a pattern occurrence in a given text?
Explanation: The Z-algorithm constructs a new string by concatenating the pattern, a unique delimiter (like '$'), and the text to avoid overlap, then computes the Z-array for this new string. Preprocessing only the pattern or only the text is insufficient for classic pattern search using Z-algorithm. The method does not use arrays of hash values—that is typical of Rabin-Karp.