Challenge - 5 Problems
String Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of string reversal using two-pointer swap
What is the output of the following C++ code that reverses a string using two pointers?
DSA C++
std::string s = "hello"; int left = 0, right = s.size() - 1; while (left < right) { char temp = s[left]; s[left] = s[right]; s[right] = temp; left++; right--; } std::cout << s << std::endl;
Attempts:
2 left
💡 Hint
Think about swapping characters from the start and end moving towards the center.
✗ Incorrect
The code swaps characters from the start and end moving inward, effectively reversing the string.
❓ Predict Output
intermediateOutput of string reversal using recursion
What is the output of this recursive function that reverses a string?
DSA C++
std::string reverseString(const std::string& s) {
if (s.empty()) return "";
return reverseString(s.substr(1)) + s[0];
}
int main() {
std::string s = "abc";
std::cout << reverseString(s) << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
The function calls itself with the substring excluding the first character and appends the first character at the end.
✗ Incorrect
The recursion builds the reversed string by moving the first character to the end after reversing the rest.
🔧 Debug
advancedIdentify the error in this string reversal code
What error does this code produce when trying to reverse a string?
DSA C++
std::string s = "test"; for (int i = 0; i <= s.size() / 2; i++) { char temp = s[i]; s[i] = s[s.size() - i - 1]; s[s.size() - i - 1] = temp; } std::cout << s << std::endl;
Attempts:
2 left
💡 Hint
Check the index used for swapping characters carefully.
✗ Incorrect
The code accesses s[s.size() - i], which is out of range because string indices go from 0 to size()-1.
🧠 Conceptual
advancedWhich approach uses O(n) extra space for string reversal?
Among these string reversal methods, which one uses extra space proportional to the string length?
Attempts:
2 left
💡 Hint
Consider which method creates new strings during the process.
✗ Incorrect
Recursion with substring concatenation creates new strings at each call, using O(n) extra space.
🚀 Application
expertFinal string state after multiple reversal operations
Given the following operations on string s, what is the final string?
DSA C++
std::string s = "algorithm"; // Operation 1: Reverse entire string std::reverse(s.begin(), s.end()); // Operation 2: Reverse substring from index 2 to 5 (inclusive) std::reverse(s.begin() + 2, s.begin() + 6); // Operation 3: Reverse substring from index 0 to 3 (inclusive) std::reverse(s.begin(), s.begin() + 4); std::cout << s << std::endl;
Attempts:
2 left
💡 Hint
Apply each reversal step carefully on the string.
✗ Incorrect
Step 1 reverses entire string to 'mhtirogla'. Step 2 reverses substring indices 2-5 ('t','i','r','o') to ('o','r','i','t') resulting in 'mhoritgla'. Step 3 reverses substring indices 0-3 ('m','h','o','r') to ('r','o','h','m') resulting in 'rohmitgla'.
