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 StringBuilder
What is the output of the following Java code snippet?
DSA Java
public class Main { public static void main(String[] args) { String original = "hello"; String reversed = new StringBuilder(original).reverse().toString(); System.out.println(reversed); } }
Attempts:
2 left
💡 Hint
StringBuilder has a reverse() method that reverses the characters.
✗ Incorrect
The StringBuilder reverse() method reverses the string characters. So 'hello' becomes 'olleh'.
❓ Predict Output
intermediateOutput of manual character swap reversal
What is the output of this Java code that reverses a string by swapping characters?
DSA Java
public class Main { public static void main(String[] args) { char[] chars = {'a', 'b', 'c', 'd'}; int left = 0, right = chars.length - 1; while (left < right) { char temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; left++; right--; } System.out.println(new String(chars)); } }
Attempts:
2 left
💡 Hint
Swapping characters from ends moves them to reverse positions.
✗ Incorrect
The code swaps characters from start and end moving inward, reversing the array. Output is 'dcba'.
❓ Predict Output
advancedOutput of recursive string reversal function
What is printed when this recursive Java method is called with input "abc"?
DSA Java
public class Main { public static String reverse(String s) { if (s.isEmpty()) { return s; } return reverse(s.substring(1)) + s.charAt(0); } public static void main(String[] args) { System.out.println(reverse("abc")); } }
Attempts:
2 left
💡 Hint
The function calls itself on substring excluding first char, then adds first char at end.
✗ Incorrect
The recursion reverses substring from index 1 onward, then appends first character, reversing the string.
❓ Predict Output
advancedOutput of String reversal using streams
What is the output of this Java code that reverses a string using streams?
DSA Java
import java.util.stream.*; public class Main { public static void main(String[] args) { String original = "java"; String reversed = IntStream.rangeClosed(1, original.length()) .mapToObj(i -> original.charAt(original.length() - i)) .map(String::valueOf) .collect(Collectors.joining()); System.out.println(reversed); } }
Attempts:
2 left
💡 Hint
The code maps indices from end to start and collects characters into a string.
✗ Incorrect
IntStream generates indices from 1 to length, maps to characters from end to start, then joins them to reverse the string.
🧠 Conceptual
expertTime complexity of different string reversal methods
Which of the following statements about time complexity of string reversal methods in Java is correct?
Attempts:
2 left
💡 Hint
Consider how many times characters are processed and cost of string concatenation.
✗ Incorrect
StringBuilder reverse() is optimized to reverse in linear time. Concatenating strings in loop is O(n^2). Recursive reversal is O(n) and Java does not guarantee tail call optimization. Streams with joining are O(n).
