Challenge - 5 Problems
String Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of slicing-based string reversal
What is the output of this Python code that reverses a string using slicing?
DSA Python
s = "hello" reversed_s = s[::-1] print(reversed_s)
Attempts:
2 left
š” Hint
Think about how slicing with a step of -1 works on strings.
ā Incorrect
The slice s[::-1] creates a new string by taking characters from the end to the start, effectively reversing it.
ā Predict Output
intermediateOutput of reversed() with join for string reversal
What is printed by this code that reverses a string using reversed() and join()?
DSA Python
s = "world" reversed_s = ''.join(reversed(s)) print(reversed_s)
Attempts:
2 left
š” Hint
reversed() returns an iterator that goes backwards over the string.
ā Incorrect
reversed(s) returns characters from the end to the start. Joining them with an empty string creates the reversed string.
ā Predict Output
advancedOutput of manual string reversal using a loop
What is the output of this code that reverses a string by building a new string in a loop?
DSA Python
s = "abcde" result = "" for char in s: result = char + result print(result)
Attempts:
2 left
š” Hint
Each new character is added before the existing result string.
ā Incorrect
By adding each character before the current result, the string is reversed step by step.
ā Predict Output
advancedOutput of in-place reversal using list conversion
What is printed after reversing a string by converting it to a list and swapping characters in place?
DSA Python
s = "python" chars = list(s) left, right = 0, len(chars) - 1 while left < right: chars[left], chars[right] = chars[right], chars[left] left += 1 right -= 1 reversed_s = ''.join(chars) print(reversed_s)
Attempts:
2 left
š” Hint
Swapping characters from ends towards the center reverses the list.
ā Incorrect
Swapping characters at left and right indices moves characters to reverse the list in place.
š§ Conceptual
expertTime complexity of different string reversal methods
Which string reversal approach has the best time complexity for reversing a string of length n?
Attempts:
2 left
š” Hint
Consider how string concatenation inside loops affects time complexity.
ā Incorrect
Concatenating strings in a loop is costly because strings are immutable, causing O(n²) time. In-place list swapping and slicing are O(n). reversed() with join() is also O(n) because join is optimized.
