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 Array Reverse
What is the output of the following C# code that reverses a string using an array?
DSA C#
string input = "hello"; char[] arr = input.ToCharArray(); Array.Reverse(arr); string reversed = new string(arr); Console.WriteLine(reversed);
Attempts:
2 left
💡 Hint
Think about what Array.Reverse does to the character array.
✗ Incorrect
The code converts the string to a char array, reverses it in place, then creates a new string from the reversed array. The output is the reversed string 'olleh'.
❓ Predict Output
intermediateOutput of String Reversal Using StringBuilder
What does this C# code print when reversing a string using StringBuilder?
DSA C#
string input = "world"; var sb = new System.Text.StringBuilder(); for (int i = input.Length - 1; i >= 0; i--) { sb.Append(input[i]); } Console.WriteLine(sb.ToString());
Attempts:
2 left
💡 Hint
The loop appends characters from the end to the start.
✗ Incorrect
The loop appends characters from the last index to the first, building the reversed string 'dlrow'.
🔧 Debug
advancedIdentify the Error in Recursive String Reversal
What error does this recursive string reversal code produce when run?
DSA C#
string Reverse(string s) {
if (s.Length == 0) return s;
return Reverse(s.Substring(1)) + s[0];
}
Console.WriteLine(Reverse(null));Attempts:
2 left
💡 Hint
Consider what happens when s is null and you call s.Length.
✗ Incorrect
Calling s.Length when s is null causes a NullReferenceException.
🧠 Conceptual
advancedTime Complexity of Different String Reversal Methods
Which option correctly states the time complexity of reversing a string of length n using a for-loop appending characters to a StringBuilder?
Attempts:
2 left
💡 Hint
Appending to StringBuilder is efficient; consider how many times the loop runs.
✗ Incorrect
The loop runs n times and appending to StringBuilder is O(1) amortized, so total is O(n).
🚀 Application
expertOutput of In-Place Character Swap String Reversal
What is the output of this C# code that reverses a string by swapping characters in place?
DSA C#
char[] chars = "abcde".ToCharArray(); int left = 0, right = chars.Length - 1; while (left < right) { char temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; left++; right--; } Console.WriteLine(new string(chars));
Attempts:
2 left
💡 Hint
Swapping characters from ends moves inward to reverse the array.
✗ Incorrect
The code swaps characters from the start and end moving inward, reversing the array to 'edcba'.
