Bird
Raised Fist0
DSA C++programming~20 mins

String Reversal Approaches in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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;
Aolleh
Bhello
CSyntaxError
Dohlle
Attempts:
2 left
💡 Hint
Think about swapping characters from the start and end moving towards the center.
❓ Predict Output
intermediate
2:00remaining
Output 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;
}
Aabc
BRuntimeError
CSyntaxError
Dcba
Attempts:
2 left
💡 Hint
The function calls itself with the substring excluding the first character and appends the first character at the end.
🔧 Debug
advanced
2:00remaining
Identify 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;
AOut of range error
BNo error, outputs 'tset'
CSyntax error
DInfinite loop
Attempts:
2 left
💡 Hint
Check the index used for swapping characters carefully.
🧠 Conceptual
advanced
2:00remaining
Which approach uses O(n) extra space for string reversal?
Among these string reversal methods, which one uses extra space proportional to the string length?
AIn-place two-pointer swap
BUsing recursion with substring concatenation
CUsing std::reverse from <algorithm>
DSwapping characters with a temporary variable
Attempts:
2 left
💡 Hint
Consider which method creates new strings during the process.
🚀 Application
expert
3:00remaining
Final 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;
Amhtirogla
Bmhtorgila
Crohmitgla
Dmhtroigla
Attempts:
2 left
💡 Hint
Apply each reversal step carefully on the string.

Practice

(1/5)
1. What does the std::reverse function do when applied to a string in C++?
easy
A. It reverses the order of characters in the string.
B. It sorts the characters in ascending order.
C. It removes all vowels from the string.
D. It converts all characters to uppercase.

Solution

  1. Step 1: Understand std::reverse behavior

    The std::reverse function reverses the order of elements in a range, here characters in a string.
  2. Step 2: Apply to string

    When applied to a string, it changes the string so the last character becomes first, and so on.
  3. Final Answer:

    It reverses the order of characters in the string. -> Option A
  4. Quick Check:

    std::reverse reverses string characters [OK]
Hint: std::reverse flips string characters in place [OK]
Common Mistakes:
  • Thinking std::reverse sorts characters
  • Confusing reversal with uppercase conversion
  • Assuming it removes characters
2. Which of the following is the correct syntax to reverse a string s using std::reverse in C++?
easy
A. std::reverse(s.begin(), s.end());
B. std::reverse(s, 0, s.length());
C. reverse(s.begin(), s.end());
D. std::reverse(s.start(), s.finish());

Solution

  1. Step 1: Identify correct std::reverse usage

    The std::reverse function requires two iterators: start and end of the range.
  2. Step 2: Match syntax with string iterators

    For string s, s.begin() and s.end() provide correct iterators.
  3. Final Answer:

    std::reverse(s.begin(), s.end()); -> Option A
  4. Quick Check:

    std::reverse needs begin and end iterators [OK]
Hint: Use s.begin() and s.end() with std::reverse [OK]
Common Mistakes:
  • Using incorrect iterator names like start() or finish()
  • Calling reverse without std:: prefix
  • Passing string and indices directly
3. What will be the output of the following C++ code?
std::string s = "hello";
for (int i = 0; i < s.length() / 2; ++i) {
    std::swap(s[i], s[s.length() - 1 - i]);
}
std::cout << s;
medium
A. hello
B. helol
C. olleh
D. error

Solution

  1. Step 1: Understand the swapping loop

    The loop swaps characters from start and end moving towards the center, effectively reversing the string.
  2. Step 2: Trace the swaps on "hello"

    Swap s[0] and s[4]: 'h' and 'o' -> "oellh"; Swap s[1] and s[3]: 'e' and 'l' -> "olleh".
  3. Final Answer:

    olleh -> Option C
  4. Quick Check:

    Swapping pairs reverses string [OK]
Hint: Swapping pairs from ends reverses string [OK]
Common Mistakes:
  • Not swapping correctly causing partial reversal
  • Loop running too many or too few times
  • Confusing indices causing wrong output
4. Identify the error in the following code that attempts to reverse a string s.
for (int i = 0; i <= s.length() / 2; ++i) {
    std::swap(s[i], s[s.length() - i]);
}
medium
A. No error; code correctly reverses the string.
B. The second index should be s.length() - 1 - i instead of s.length() - i to avoid out-of-range access.
C. The loop should start from i = 1 instead of 0.
D. std::swap cannot be used with string characters.

Solution

  1. Step 1: Check the swap indices

    The code uses s[s.length() - i]. For i=0, this accesses s[s.length()], out of bounds.
  2. Step 2: Correct reversal index

    To reverse, swap s[i] with s.length() - 1 - i.
  3. Final Answer:

    The second index should be s.length() - 1 - i instead of s.length() - i to avoid out-of-range access. -> Option B
  4. Quick Check:

    Missing -1 in second index causes out-of-range [OK]
Hint: Check indices carefully to avoid out-of-range errors [OK]
Common Mistakes:
  • Off-by-one error: s.length() - i instead of s.length() - 1 - i
  • Incorrect index calculation for swap
  • Assuming std::swap can't swap chars
5. Given a string s, which approach below correctly reverses the string without using extra space and handles empty strings safely?
hard
A. Use a while loop swapping characters until i > s.length().
B. Use std::reverse(s.begin(), s.end() - 1); to reverse all but last character.
C. Create a new string and append characters from end to start.
D. Use a for loop swapping characters from start to end with condition i < s.length() / 2.

Solution

  1. Step 1: Analyze each approach

    Use a for loop swapping characters from start to end with condition i < s.length() / 2. swaps characters in place up to half length, safe for empty strings. Use std::reverse(s.begin(), s.end() - 1); to reverse all but last character. misses last character. Create a new string and append characters from end to start. uses extra space. Use a while loop swapping characters until i > s.length(). has wrong loop condition.
  2. Step 2: Confirm correctness and safety

    Use a for loop swapping characters from start to end with condition i < s.length() / 2. reverses string in place without extra space and works for empty strings as loop won't run.
  3. Final Answer:

    Use a for loop swapping characters from start to end with condition i < s.length() / 2. -> Option D
  4. Quick Check:

    In-place swap up to half length reverses string safely [OK]
Hint: Swap pairs up to half length for safe in-place reversal [OK]
Common Mistakes:
  • Using incorrect end iterator causing partial reversal
  • Creating new string uses extra space
  • Wrong loop conditions causing infinite loops
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard