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 C++ code that searches for all occurrences of a pattern in a text using the naive substring search method?
DSA C++
std::string text = "abracadabra"; std::string pattern = "abra"; for (int i = 0; i <= (int)text.size() - (int)pattern.size(); i++) { int j = 0; while (j < (int)pattern.size() && text[i + j] == pattern[j]) { j++; } if (j == (int)pattern.size()) { std::cout << i << " "; } } std::cout << std::endl;
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 checks each position and prints indices where full match occurs.
❓ Predict Output
intermediateOutput of KMP Prefix Table Construction
What is the output of the prefix table (also called lps array) constructed by the KMP algorithm for the pattern "ababc"?
DSA C++
std::string pattern = "ababc"; int n = pattern.size(); std::vector<int> lps(n, 0); int len = 0; int i = 1; while (i < n) { if (pattern[i] == pattern[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } for (int x : lps) std::cout << x << " "; std::cout << std::endl;
Attempts:
2 left
💡 Hint
The prefix table stores the length of the longest proper prefix which is also suffix for each prefix of the pattern.
✗ Incorrect
For pattern "ababc", the lps array is [0,0,1,2,0]. This means at index 2, the longest prefix-suffix length is 1, at index 3 it is 2, and so on.
🧠 Conceptual
advancedTime Complexity of Rabin-Karp Algorithm
Which of the following best describes the average and worst-case time complexity of the Rabin-Karp substring search algorithm when searching for a pattern of length m in a text of length n?
Attempts:
2 left
💡 Hint
Consider how hashing helps average case but collisions cause worst case.
✗ Incorrect
Rabin-Karp uses hashing to find matches quickly on average (O(n+m)). However, in worst case, many hash collisions cause it to check all substrings, leading to O(n*m).
❓ Predict Output
advancedOutput of Boyer-Moore Bad Character Table
What is the output of the bad character table for the pattern "example" assuming ASCII characters and the table stores the last occurrence index of each character in the pattern?
DSA C++
std::string pattern = "example"; const int ALPHABET_SIZE = 256; std::vector<int> badChar(ALPHABET_SIZE, -1); for (int i = 0; i < (int)pattern.size(); i++) { badChar[(unsigned char)pattern[i]] = i; } std::cout << badChar['e'] << " " << badChar['x'] << " " << badChar['a'] << " " << badChar['m'] << " " << badChar['p'] << " " << badChar['l'] << " " << badChar['z'] << std::endl;
Attempts:
2 left
💡 Hint
The bad character table stores the last index of each character in the pattern.
✗ Incorrect
In "example", 'e' appears last at index 6, 'x' at 1, 'a' at 2, 'm' at 3, 'p' at 4, 'l' at 5, and 'z' does not appear (-1).
🧠 Conceptual
expertChoosing Substring Search Algorithm for Large Texts
You need to search for multiple patterns in a very large text efficiently. Which substring search approach is best suited for this scenario?
Attempts:
2 left
💡 Hint
Consider an algorithm designed for multiple pattern searches simultaneously.
✗ Incorrect
Aho-Corasick builds a trie automaton for all patterns and searches the text once, efficiently finding all pattern occurrences. Other methods search separately, which is inefficient for many patterns.
