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(const string &s, int start, int end) {
while (start < end) {
if (s[start] != s[end]) {
return [1];
}
start++;
end--;
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning true when characters don't match.
Returning start or end instead of a boolean.
✗ Incorrect
If characters at start and end don't match, the substring is not a palindrome, so return false.
2fill in blank
mediumComplete the code to add the current substring to the path vector.
DSA C++
void backtrack(const string &s, int start, vector<string> &path, vector<vector<string>> &result) {
if (start == s.size()) {
result.push_back(path);
return;
}
for (int end = start; end < s.size(); end++) {
if (isPalindrome(s, start, end)) {
path.[1](s.substr(start, end - start + 1));
backtrack(s, end + 1, path, result);
path.pop_back();
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using append which is not valid for vector.
Using insert without specifying position.
✗ Incorrect
In C++, vector uses push_back to add elements at the end.
3fill in blank
hardFix the error in the base case condition to check if we reached the end of the string.
DSA C++
void backtrack(const string &s, int start, vector<string> &path, vector<vector<string>> &result) {
if (start [1] s.size()) {
result.push_back(path);
return;
}
for (int end = start; end < s.size(); end++) {
if (isPalindrome(s, start, end)) {
path.push_back(s.substr(start, end - start + 1));
backtrack(s, end + 1, path, result);
path.pop_back();
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < which can cause out-of-bound errors.
Using != which is incorrect for this base case.
✗ Incorrect
We want to check if start reached the string length, meaning all characters are processed.
4fill in blank
hardFill both blanks to create a map of substring lengths to substrings that are palindromes.
DSA C++
unordered_map<int, vector<string>> palindromeMap; for (int i = 0; i < s.size(); i++) { for (int j = i; j < s.size(); j++) { if (isPalindrome(s, i, j)) { palindromeMap[[1]].push_back(s.substr(i, [2])); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using j - i which is off by one.
Using i + j which is not length.
✗ Incorrect
The length of the substring is j - i + 1, used as the key to group palindromes by length.
5fill in blank
hardFill all three blanks to create a filtered map that only includes palindromes longer than 2 characters.
DSA C++
auto filteredMap = unordered_map<int, vector<string>>{};
for (const auto& [key, substrings] : palindromeMap) {
if (key [1] 2) {
filteredMap[[2]] = substrings;
}
}
for (const auto& [key, val] : filteredMap) {
cout << "Length: " << [3] << " -> " << val.size() << " substrings\n";
}Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in condition.
Using length instead of key in map assignment and print.
✗ Incorrect
We filter lengths greater than 2, assign to filteredMap using key, and print key in output.
