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 TypeScript code that performs a naive substring search?
DSA Typescript
function naiveSearch(text: string, pattern: string): number[] {
const result: number[] = [];
for (let i = 0; i <= text.length - pattern.length; i++) {
let j = 0;
while (j < pattern.length && text[i + j] === pattern[j]) {
j++;
}
if (j === pattern.length) {
result.push(i);
}
}
return result;
}
const text = "abracadabra";
const pattern = "abra";
console.log(naiveSearch(text, pattern));Attempts:
2 left
💡 Hint
Check where the pattern "abra" appears exactly in the text "abracadabra".
✗ Incorrect
The pattern "abra" appears at index 0 and index 7 in the text. The naive search checks each position and finds matches only at these two indices.
❓ Predict Output
intermediateOutput of KMP Prefix Table Construction
What is the output of the prefix table (also called lps array) constructed by the following TypeScript function for the pattern "ababc"?
DSA Typescript
function buildPrefixTable(pattern: string): number[] {
const lps: number[] = new Array(pattern.length).fill(0);
let length = 0;
let i = 1;
while (i < pattern.length) {
if (pattern[i] === pattern[length]) {
length++;
lps[i] = length;
i++;
} else {
if (length !== 0) {
length = lps[length - 1];
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
console.log(buildPrefixTable("ababc"));Attempts:
2 left
💡 Hint
The prefix table stores the length of the longest proper prefix which is also a suffix for each substring.
✗ Incorrect
For "ababc", the prefix table 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
What is the 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 the case when hash collisions happen for every substring.
✗ Incorrect
In the worst case, Rabin-Karp compares the pattern with every substring of the text due to hash collisions, resulting in O(n * m) time.
🔧 Debug
advancedIdentify the Bug in KMP Search Implementation
Given the following TypeScript code for KMP substring search, which option correctly identifies the bug causing incorrect search results?
DSA Typescript
function kmpSearch(text: string, pattern: string): number[] {
const lps = buildPrefixTable(pattern);
const result: number[] = [];
let i = 0, j = 0;
while (i < text.length) {
if (text[i] === pattern[j]) {
i++;
j++;
}
if (j === pattern.length) {
result.push(i - j);
j = 0; // Bug here
} else if (i < text.length && text[i] !== pattern[j]) {
if (j !== 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
return result;
}
function buildPrefixTable(pattern: string): number[] {
const lps: number[] = new Array(pattern.length).fill(0);
let length = 0;
let i = 1;
while (i < pattern.length) {
if (pattern[i] === pattern[length]) {
length++;
lps[i] = length;
i++;
} else {
if (length !== 0) {
length = lps[length - 1];
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
console.log(kmpSearch("ababcababc", "ababc"));Attempts:
2 left
💡 Hint
Check how j is updated after finding a match to allow overlapping matches.
✗ Incorrect
After a match, j should be set to lps[j-1] to allow searching for overlapping patterns. Resetting j to 0 misses these.
🚀 Application
expertFinding All Anagram Indices Using Sliding Window
You want to find all start indices of substrings in a text that are anagrams of a given pattern. Which approach below correctly implements this using a sliding window technique in TypeScript?
Attempts:
2 left
💡 Hint
Anagrams have the same character counts, so frequency comparison is key.
✗ Incorrect
Sliding window with frequency arrays allows O(n) time complexity by updating counts incrementally and comparing efficiently.
