Challenge - 5 Problems
Substring Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Naive Substring Search
What is the output of the following Java code that searches for a pattern in a text using the naive substring search method?
DSA Java
public class NaiveSearch { public static void main(String[] args) { String text = "abracadabra"; String pattern = "abra"; for (int i = 0; i <= text.length() - pattern.length(); i++) { int j; for (j = 0; j < pattern.length(); j++) { if (text.charAt(i + j) != pattern.charAt(j)) { break; } } if (j == pattern.length()) { System.out.print(i + " "); } } } }
Attempts:
2 left
💡 Hint
Check where the pattern "abra" fully matches in the text "abracadabra".
✗ Incorrect
The pattern "abra" appears at index 0 and again at index 7 in the text. The naive search prints these starting indices.
❓ Predict Output
intermediateOutput of KMP Prefix Table Construction
What is the output of the following Java code that builds the prefix table (lps array) for the pattern "ABABAC" using the KMP algorithm?
DSA Java
public class KMPPrefix { public static void main(String[] args) { String pattern = "ABABAC"; int[] lps = new int[pattern.length()]; int len = 0; int i = 1; while (i < pattern.length()) { if (pattern.charAt(i) == pattern.charAt(len)) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } for (int val : lps) { System.out.print(val + " "); } } }
Attempts:
2 left
💡 Hint
The prefix table stores lengths of longest proper prefix which is also suffix for each prefix of the pattern.
✗ Incorrect
For pattern "ABABAC", the lps array is [0,0,1,2,3,0]. This means at index 4, the longest prefix-suffix length is 3, and at index 5 it resets to 0.
🔧 Debug
advancedIdentify the Error in Rabin-Karp Hash Calculation
Consider the following Java code snippet for Rabin-Karp substring search. What error will it cause when run?
DSA Java
public class RabinKarp { public static void main(String[] args) { String text = "hellohello"; String pattern = "hello"; int prime = 101; int patternHash = 0, textHash = 0; int m = pattern.length(); int h = 1; for (int i = 0; i < m - 1; i++) { h = (h * 256) % prime; } for (int i = 0; i < m; i++) { patternHash = (256 * patternHash + pattern.charAt(i)) % prime; textHash = (256 * textHash + text.charAt(i)) % prime; } for (int i = 0; i <= text.length() - m; i++) { if (patternHash == textHash) { int j; for (j = 0; j < m; j++) { if (text.charAt(i + j) != pattern.charAt(j)) { break; } } if (j == m) { System.out.print(i + " "); } } if (i < text.length() - m) { textHash = (256 * (textHash - text.charAt(i) * h) + text.charAt(i + m)) % prime; if (textHash < 0) { textHash += prime; } } } } }
Attempts:
2 left
💡 Hint
Check how negative hash values are handled after rolling hash update.
✗ Incorrect
The code correctly handles negative hash values by adding prime if textHash < 0. It prints indices 0 and 5 where pattern matches.
❓ Predict Output
advancedOutput of Z-Algorithm for Pattern Matching
What is the output of the following Java code that computes the Z-array for the string "aabcaabxaaaz"?
DSA Java
public class ZAlgorithm { public static void main(String[] args) { String s = "aabcaabxaaaz"; int n = s.length(); int[] Z = new int[n]; int L = 0, R = 0; for (int i = 1; i < n; i++) { if (i > R) { L = R = i; while (R < n && s.charAt(R - L) == s.charAt(R)) { R++; } Z[i] = R - L; R--; } else { int k = i - L; if (Z[k] < R - i + 1) { Z[i] = Z[k]; } else { L = i; while (R < n && s.charAt(R - L) == s.charAt(R)) { R++; } Z[i] = R - L; R--; } } } for (int val : Z) { System.out.print(val + " "); } } }
Attempts:
2 left
💡 Hint
Z-array at position i shows length of substring starting at i matching prefix.
✗ Incorrect
The Z-array for "aabcaabxaaaz" is [0,2,0,0,3,1,0,0,2,1,1,0]. This matches the longest prefix matches at each position.
🧠 Conceptual
expertTime Complexity Comparison of Substring Search Algorithms
Which of the following statements correctly compares the worst-case time complexities of naive substring search, KMP algorithm, and Rabin-Karp algorithm for searching a pattern of length m in a text of length n?
Attempts:
2 left
💡 Hint
Consider how each algorithm handles pattern matching and worst-case scenarios.
✗ Incorrect
Naive search checks every position leading to O(n*m). KMP preprocesses pattern for O(n) search. Rabin-Karp average O(n+m), but worst-case hash collisions cause O(n*m).
