Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a substring is a palindrome.
DSA C#
bool IsPalindrome(string s, int start, int end) {
while (start < end) {
if (s[start] != s[[1]]) return false;
start++;
end--;
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using s[start + 1] instead of s[end]
Using s[end - 1] or s[start]
✗ Incorrect
To check palindrome, compare characters from start and end moving towards center. So, s[start] is compared with s[end], then end moves left by 1 (end - 1).
2fill in blank
mediumComplete the code to add the current substring to the path list.
DSA C#
void Backtrack(string s, int start, List<string> path, List<List<string>> result) {
if (start == s.Length) {
result.Add(new List<string>(path));
return;
}
for (int end = start; end < s.Length; end++) {
if (IsPalindrome(s, start, end)) {
path.[1](s.Substring(start, end - start + 1));
Backtrack(s, end + 1, path, result);
path.RemoveAt(path.Count - 1);
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Remove instead of Add
Using Clear which empties the list
✗ Incorrect
We add the current palindrome substring to the path before exploring further.
3fill in blank
hardFix the error in the recursive call to continue partitioning.
DSA C#
Backtrack(s, [1], path, result);Attempts:
3 left
💡 Hint
Common Mistakes
Using end instead of end + 1 causes infinite recursion
Using start or start + 1 is incorrect here
✗ Incorrect
After choosing a palindrome substring from start to end, we move to the next index end + 1 for further partitioning.
4fill in blank
hardFill both blanks to complete the main function that starts backtracking.
DSA C#
List<List<string>> Partition(string s) {
var result = new List<List<string>>();
var path = new List<string>();
[1](s, 0, path, result);
return [2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning path instead of result
Calling IsPalindrome instead of Backtrack
✗ Incorrect
We call Backtrack to start partitioning from index 0 and return the result list.
5fill in blank
hardFill all three blanks to complete the backtracking loop and palindrome check.
DSA C#
for (int end = [1]; end < s.Length; end++) { if ([2](s, start, [3])) { path.Add(s.Substring(start, end - start + 1)); Backtrack(s, end + 1, path, result); path.RemoveAt(path.Count - 1); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 instead of start
Using wrong method name for palindrome check
Using wrong variable in loop condition
✗ Incorrect
The loop starts from start index, checks palindrome using IsPalindrome, and uses end as loop variable.
