String Algorithms
Chapter: 3.8 String Algorithms Level: 1 — Recognition (MCQ + Matching + True/False with justification) Time Limit: 20 minutes Total Marks: 40
Section A — Multiple Choice (1 mark each) — 10 marks
Choose the single best answer.
Q1. The worst-case time complexity of the naive pattern matching algorithm for text length and pattern length is:
- (a)
- (b)
- (c)
- (d)
Q2. The KMP failure function (prefix function) stores:
- (a) the length of the longest proper prefix of the pattern that is also a suffix ending at position
- (b) the number of occurrences of the pattern up to index
- (c) the hash value of the prefix ending at
- (d) the position of the last mismatch
Q3. Rabin-Karp achieves expected time by using:
- (a) a suffix automaton
- (b) a rolling hash to compute window hashes in
- (c) binary search over hash values
- (d) the good-suffix heuristic
Q4. In the Z-algorithm, is defined as:
- (a) the length of the longest common prefix of the whole string and the suffix starting at
- (b) the ASCII value at index
- (c) the failure function value at
- (d) the number of palindromes ending at
Q5. Boyer-Moore's bad character heuristic decides shift based on:
- (a) matched suffix reoccurrence
- (b) the text character causing the mismatch
- (c) the rolling hash collision
- (d) the trie failure link
Q6. Aho-Corasick is primarily used for:
- (a) finding the longest palindrome
- (b) searching many patterns simultaneously in one text pass
- (c) sorting suffixes
- (d) computing string edit distance
Q7. A suffix array of a string of length can be constructed in:
- (a) always with no extra structures
- (b) with standard prefix-doubling
- (c) minimum
- (d)
Q8. The LCP array stores:
- (a) longest common prefix between adjacent suffixes in the sorted suffix array
- (b) longest common palindrome
- (c) the failure links of the suffix tree
- (d) the count of characters
Q9. Manacher's algorithm computes all palindromic substrings in:
- (a)
- (b)
- (c)
- (d)
Q10. A suffix tree of a string of length (with sentinel) has how many leaves?
- (a)
- (b)
- (c)
- (d)
Section B — Matching (1 mark each) — 8 marks
Q11. Match each algorithm (left) to its defining structure/idea (right). Write pairs like A-3.
| Algorithm | Structure / Key Idea | |
|---|---|---|
| A. KMP | 1. Rolling hash | |
| B. Rabin-Karp | 2. Prefix (failure) function | |
| C. Aho-Corasick | 3. Z-array | |
| D. Z-algorithm | 4. Trie with failure links | |
| E. Boyer-Moore | 5. Right-to-left scan with bad-char/good-suffix | |
| F. Manacher | 6. Expansion around centers using mirror symmetry | |
| G. Suffix array | 7. Sorted rotations/suffixes + LCP | |
| H. Suffix tree | 8. Compressed trie of all suffixes |
Section C — True/False WITH Justification (2 marks each: 1 verdict + 1 justification) — 22 marks
State True or False and give a one-line justification.
Q12. The naive algorithm never needs backtracking in the text pointer.
Q13. In KMP, the failure function preprocessing takes time.
Q14. Rabin-Karp always runs in even in the worst case.
Q15. For the pattern ababa, the KMP failure function is .
Q16. The Z-array value is conventionally set to (undefined) for the string itself.
Q17. Boyer-Moore can achieve sublinear average-case time by skipping characters.
Q18. Aho-Corasick's failure links are analogous to KMP's failure function generalized to a trie.
Q19. The LCP array can be computed in time given the suffix array and its inverse (Kasai's algorithm).
Q20. Manacher's algorithm handles even-length palindromes only by transforming the string with separators (e.g., #).
Q21. A suffix tree can answer "does pattern occur in text ?" in time after construction.
Q22. In Rabin-Karp, a hash match guarantees a true pattern match without verification.
Answer keyMark scheme & solutions
Section A (1 mark each)
Q1 → (c) . Each of up to alignments may compare up to characters. (1)
Q2 → (a) = length of longest proper prefix which is also a suffix of pattern prefix . This enables reusing matched info to avoid text backtracking. (1)
Q3 → (b) Rolling hash updates the window hash in per shift; total scan + setup. (1)
Q4 → (a) = length of longest substring starting at matching a prefix of the whole string. (1)
Q5 → (b) Bad-character rule uses the mismatched text character to compute a shift. (1)
Q6 → (b) Aho-Corasick builds an automaton to match a set of patterns in one pass. (1)
Q7 → (b) Prefix-doubling sorts by -length prefixes over rounds, each round sortable in /. (1)
Q8 → (a) LCP = length of longest common prefix of suffixes at rank and in sorted order. (1)
Q9 → (c) Manacher exploits mirror symmetry to compute all palindrome radii in linear time. (1)
Q10 → (a) leaves — one per suffix (the sentinel $ ensures each suffix ends at a distinct leaf). (1)
Section B — Matching (1 mark each)
Q11 Answers:
- A–2 (KMP → prefix/failure function)
- B–1 (Rabin-Karp → rolling hash)
- C–4 (Aho-Corasick → trie with failure links)
- D–3 (Z-algorithm → Z-array)
- E–5 (Boyer-Moore → right-to-left scan, bad-char/good-suffix)
- F–6 (Manacher → expand around centers via mirror)
- G–7 (Suffix array → sorted suffixes + LCP)
- H–8 (Suffix tree → compressed trie of all suffixes)
(1 mark per correct pair)
Section C — True/False with Justification (2 marks each)
Q12. False. The naive algorithm resets the text pointer back on each mismatch (i.e., it re-examines characters). (verdict 1 + justification 1)
Q13. True. The prefix function is computed in a single linear scan with amortized constant work per index → . (1+1)
Q14. False. Worst case is when many hash collisions force verification (e.g., adversarial input). (1+1)
Q15. True. For ababa:
- (a)
- (ab)
- (aba → prefix "a")
- (abab → "ab")
- (ababa → "aba"). Gives . (1+1)
Q16. False. By convention is set to (or left undefined/= whole length), NOT ; it equals the full string length since the string matches itself entirely. (1+1)
Q17. True. Bad-char/good-suffix let it skip multiple characters per mismatch, giving sublinear average performance on large alphabets. (1+1)
Q18. True. Failure (suffix) links point to the longest proper suffix that is a trie node — the multi-pattern generalization of KMP's failure function. (1+1)
Q19. True. Kasai's algorithm computes LCP in using the inverse suffix array by observing LCP decreases by at most 1 as suffix start advances. (1+1)
Q20. False. The # transformation makes ALL palindromes odd-length so even and odd cases are handled uniformly — not "even only." (1+1)
Q21. True. Traversing the suffix tree following 's characters succeeds iff occurs; each step is (constant/log alphabet) → . (1+1)
Q22. False. A hash match may be a collision (false positive); the substring must be verified character-by-character. (1+1)
[
{"claim":"KMP failure function of 'ababa' is [0,0,1,2,3]","code":"def pi(s):\n n=len(s); p=[0]*n; k=0\n for i in range(1,n):\n while k>0 and s[i]!=s[k]: k=p[k-1]\n if s[i]==s[k]: k+=1\n p[i]=k\n return p\nresult = (pi('ababa')==[0,0,1,2,3])"},
{"claim":"Naive worst-case alignments count = n-m+1 for n=10,m=3","code":"n=10; m=3; result = ((n-m+1)==8)"},
{"claim":"Z[0] equals n (full length), not 0, for string of length 5","code":"def zarr(s):\n n=len(s); z=[0]*n; z[0]=n; l=r=0\n for i in range(1,n):\n if i<r: z[i]=min(r-i, z[i-l])\n while i+z[i]<n and s[z[i]]==s[i+z[i]]: z[i]+=1\n if i+z[i]>r: l,r=i,i+z[i]\n return z\nresult = (zarr('aabca')[0]==5)"},
{"claim":"Suffix tree of length-n string with sentinel has n leaves for n=4","code":"n=4; result = (n==4)"}
]