Challenge - 5 Problems
Palindrome Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Palindrome Partitioning for 'aab'
What is the output of the following TypeScript code that finds all palindrome partitions of the string 'aab'?
DSA Typescript
function isPalindrome(s: string, left: number, right: number): boolean {
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}
function backtrack(s: string, start: number, path: string[], result: string[][]): void {
if (start === s.length) {
result.push([...path]);
return;
}
for (let end = start; end < s.length; end++) {
if (isPalindrome(s, start, end)) {
path.push(s.substring(start, end + 1));
backtrack(s, end + 1, path, result);
path.pop();
}
}
}
function partition(s: string): string[][] {
const result: string[][] = [];
backtrack(s, 0, [], result);
return result;
}
console.log(partition('aab'));Attempts:
2 left
💡 Hint
Think about all ways to split the string where each part is a palindrome.
✗ Incorrect
The code finds all partitions where each substring is a palindrome. For 'aab', the valid partitions are ['a','a','b'] and ['aa','b'].
🧠 Conceptual
intermediate1:30remaining
Number of Palindrome Partitions for 'racecar'
How many palindrome partitions does the string 'racecar' have using backtracking?
Attempts:
2 left
💡 Hint
Consider all ways to split 'racecar' into palindromic substrings.
✗ Incorrect
The string 'racecar' has 4 palindrome partitions including single letters and larger palindromes like 'racecar' itself.
🔧 Debug
advanced2:00remaining
Identify the Bug in Palindrome Partitioning Code
What error does the following TypeScript code produce when run with input 'abc'?
DSA Typescript
function isPalindrome(s: string, left: number, right: number): boolean {
while (left <= right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}
function backtrack(s: string, start: number, path: string[], result: string[][]): void {
if (start === s.length) {
result.push([...path]);
return;
}
for (let end = start; end < s.length; end++) {
if (isPalindrome(s, start, end)) {
path.push(s.substring(start, end + 1));
backtrack(s, end + 1, path, result);
path.pop();
}
}
}
function partition(s: string): string[][] {
const result: string[][] = [];
backtrack(s, 0, [], result);
return result;
}
console.log(partition('abc'));Attempts:
2 left
💡 Hint
Check the palindrome check condition carefully.
✗ Incorrect
The code runs correctly and outputs [['a','b','c']] because each single character is a palindrome.
❓ Predict Output
advanced2:00remaining
Output of Palindrome Partitioning for 'aaa'
What is the output of the following TypeScript code that partitions 'aaa' into palindromes?
DSA Typescript
function isPalindrome(s: string, left: number, right: number): boolean {
while (left < right) {
if (s[left] !== s[right]) return false;
left++;
right--;
}
return true;
}
function backtrack(s: string, start: number, path: string[], result: string[][]): void {
if (start === s.length) {
result.push([...path]);
return;
}
for (let end = start; end < s.length; end++) {
if (isPalindrome(s, start, end)) {
path.push(s.substring(start, end + 1));
backtrack(s, end + 1, path, result);
path.pop();
}
}
}
function partition(s: string): string[][] {
const result: string[][] = [];
backtrack(s, 0, [], result);
return result;
}
console.log(partition('aaa'));Attempts:
2 left
💡 Hint
Consider all palindrome substrings including overlapping ones.
✗ Incorrect
The code finds all palindrome partitions including single letters, pairs, and the whole string.
🧠 Conceptual
expert1:30remaining
Time Complexity of Palindrome Partitioning Using Backtracking
What is the time complexity of the palindrome partitioning algorithm using backtracking for a string of length n?
Attempts:
2 left
💡 Hint
Consider the number of partitions and palindrome checks.
✗ Incorrect
The algorithm explores all partitions (up to 2^(n-1)) and checks palindrome in O(n), leading to O(n * 2^n).