Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a substring is a palindrome.
DSA Javascript
function isPalindrome(s, left, right) {
while (left < right) {
if (s[left] !== s[right]) {
return false;
}
left++;
right[1];
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using right++ instead of right-=1 causes infinite loop.
Using right+=1 moves pointer in wrong direction.
✗ Incorrect
To check palindrome, we move right pointer backward by 1 each time, so right -= 1.
2fill in blank
mediumComplete the code to add a substring to the current partition.
DSA Javascript
function backtrack(start, s, currentPartition, result) {
if (start === s.length) {
result.push([...currentPartition]);
return;
}
for (let end = start; end < s.length; end++) {
if (isPalindrome(s, start, end)) {
currentPartition.[1](s.substring(start, end + 1));
backtrack(end + 1, s, currentPartition, result);
currentPartition.pop();
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using append() which is not a JavaScript array method.
Using add() which is not defined for arrays.
✗ Incorrect
In JavaScript arrays, push() adds an element to the end.
3fill in blank
hardFix the error in the base case condition to stop recursion correctly.
DSA Javascript
function backtrack(start, s, currentPartition, result) {
if (start [1] s.length) {
result.push([...currentPartition]);
return;
}
// rest of code
}Attempts:
3 left
💡 Hint
Common Mistakes
Using start < s.length causes infinite recursion.
Using start <= s.length causes out-of-bound errors.
✗ Incorrect
The base case is when start equals s.length, meaning we have partitioned the whole string.
4fill in blank
hardFill both blanks to complete the for loop and recursive call correctly.
DSA Javascript
for (let end = [1]; end < s.length; end++) { if (isPalindrome(s, start, end)) { currentPartition.push(s.substring(start, end + 1)); backtrack([2], s, currentPartition, result); currentPartition.pop(); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from end instead of start.
Calling backtrack with end instead of end + 1.
✗ Incorrect
The loop starts from start, and recursive call moves to end + 1 to continue partitioning.
5fill in blank
hardFill all three blanks to complete the palindrome partitioning function.
DSA Javascript
function partition(s) {
const result = [];
function backtrack(start, s, currentPartition, result) {
if (start === s.length) {
result.push([...currentPartition]);
return;
}
for (let end = [1]; end < s.length; end++) {
if (isPalindrome(s, start, end)) {
currentPartition.push(s.substring(start, end + 1));
backtrack([2], s, currentPartition, result);
currentPartition.pop();
}
}
}
backtrack([3], s, [], result);
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 inside recursion causes repeated work.
Calling backtrack with wrong start index.
Starting initial call from 1 instead of 0.
✗ Incorrect
Loop starts at start, recursive call moves to end + 1, and initial call starts at 0.
