Challenge - 5 Problems
Palindrome Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of expanding around center approach
What is the output of the following TypeScript code that finds the longest palindromic substring by expanding around each center?
DSA Typescript
function longestPalindrome(s: string): string {
if (s.length < 1) return "";
let start = 0, end = 0;
function expandAroundCenter(left: number, right: number): number {
while (left >= 0 && right < s.length && s[left] === s[right]) {
left--;
right++;
}
return right - left - 1;
}
for (let i = 0; i < s.length; i++) {
const len1 = expandAroundCenter(i, i);
const len2 = expandAroundCenter(i, i + 1);
const len = Math.max(len1, len2);
if (len > end - start + 1) {
start = i - Math.floor((len - 1) / 2);
end = i + Math.floor(len / 2);
}
}
return s.substring(start, end + 1);
}
console.log(longestPalindrome("babad"));Attempts:
2 left
💡 Hint
The function expands around each character and between characters to find palindromes. It returns the first longest palindrome found.
✗ Incorrect
The code expands around each center and finds "bab" and "aba" as palindromes of length 3. Since "bab" is found first, it returns "bab".
🧠 Conceptual
intermediateUnderstanding palindrome expansion boundaries
In the expandAroundCenter function, why do we return right - left - 1 as the length of the palindrome?
Attempts:
2 left
💡 Hint
Think about what happens to left and right pointers when the while loop stops.
✗ Incorrect
The while loop stops when s[left] !== s[right] or boundaries are exceeded, so left and right have moved one step beyond the palindrome. Subtracting 1 corrects the length.
🔧 Debug
advancedIdentify the error in this palindrome substring code
What error will this TypeScript code produce when run, and why?
DSA Typescript
function longestPalindrome(s: string): string {
let start = 0, maxLength = 1;
for (let i = 0; i < s.length; i++) {
for (let j = i; j < s.length; j++) {
let flag = true;
for (let k = 0; k < (j - i + 1) / 2; k++) {
if (s[i + k] !== s[j - k]) {
flag = false;
break;
}
}
if (flag && (j - i + 1) > maxLength) {
start = i;
maxLength = j - i + 1;
}
}
}
return s.substring(start, start + maxLength);
}
console.log(longestPalindrome("cbbd"));Attempts:
2 left
💡 Hint
Check the substring indices and loop conditions carefully.
✗ Incorrect
The code correctly checks all substrings and updates the longest palindrome found. It outputs "bb" for input "cbbd" without errors.
❓ Predict Output
advancedOutput of Manacher's algorithm partial implementation
What is the output of this TypeScript code snippet implementing Manacher's algorithm partial step for input "abacdfgdcaba"?
DSA Typescript
function preprocess(s: string): string {
let t = "^";
for (let c of s) {
t += "#" + c;
}
t += "#$";
return t;
}
const s = "abacdfgdcaba";
const t = preprocess(s);
console.log(t);Attempts:
2 left
💡 Hint
Manacher's algorithm inserts '#' between characters and adds '^' and '$' at ends.
✗ Incorrect
The preprocess function adds '^' at start, '#' before each character, and '$' at end, producing "^#a#b#a#c#d#f#g#d#c#a#b#a#$".
🧠 Conceptual
expertTime complexity of longest palindromic substring algorithms
Which statement correctly describes the time complexity of the common algorithms for finding the longest palindromic substring?
Attempts:
2 left
💡 Hint
Consider how many centers are expanded and how Manacher's algorithm optimizes.
✗ Incorrect
Expanding around center checks up to 2n centers with expansions up to n, resulting in O(n^2). Manacher's algorithm uses clever preprocessing and tracking to achieve O(n).
