Challenge - 5 Problems
String Reversal Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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 swapping characters?
DSA C
void reverse(char *str) { int left = 0; int right = strlen(str) - 1; while (left < right) { char temp = str[left]; str[left] = str[right]; str[right] = temp; left++; right--; } } int main() { char s[] = "hello"; reverse(s); printf("%s", s); return 0; }
Attempts:
2 left
💡 Hint
Think about swapping characters from start and end moving towards the center.
✗ Incorrect
The code swaps characters from the start and end moving inward, reversing the string 'hello' to 'olleh'.
❓ Predict Output
intermediate2:00remaining
Output of string reversal using recursion
What is the output of this C code that prints a string in reverse using recursion?
DSA C
#include <stdio.h> void reversePrint(char *str) { if (*str == '\0') return; reversePrint(str + 1); putchar(*str); } int main() { char s[] = "abc"; reversePrint(s); return 0; }
Attempts:
2 left
💡 Hint
The function calls itself with the next character before printing the current one.
✗ Incorrect
The recursion goes to the end of the string, then prints characters on the way back, reversing the output.
🧠 Conceptual
advanced2:00remaining
Why is in-place reversal more memory efficient?
Which reason best explains why reversing a string in-place (modifying the original string) is more memory efficient than creating a new reversed string?
Attempts:
2 left
💡 Hint
Think about memory allocation for new data versus modifying existing data.
✗ Incorrect
In-place reversal changes the original string without needing extra space for another string, saving memory.
🔧 Debug
advanced2:00remaining
Identify the error in this string reversal code
What error does this C code produce when trying to reverse a string literal?
DSA C
void reverse(char *str) { int left = 0; int right = strlen(str) - 1; while (left < right) { char temp = str[left]; str[left] = str[right]; str[right] = temp; left++; right--; } } int main() { char *s = "hello"; reverse(s); printf("%s", s); return 0; }
Attempts:
2 left
💡 Hint
String literals are stored in read-only memory in C.
✗ Incorrect
The code tries to modify a string literal which is stored in read-only memory, causing a runtime crash.
🚀 Application
expert2:00remaining
Result of reversing a palindrome string in-place
Given the string "madam", what is the state of the string after applying an in-place reversal function?
Attempts:
2 left
💡 Hint
A palindrome reads the same forwards and backwards.
✗ Incorrect
Reversing a palindrome string results in the same string because it reads identically both ways.
