Bird
Raised Fist0
DSA Pythonprogramming~20 mins

String Reversal Approaches in DSA Python - 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
1:00remaining
Output 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)
AError
Bolleh
Chello
Dh e l l o
Attempts:
2 left
šŸ’” Hint
Think about how slicing with a step of -1 works on strings.
ā“ Predict Output
intermediate
1:00remaining
Output 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)
Aworld
BError
C['d', 'l', 'r', 'o', 'w']
Ddlrow
Attempts:
2 left
šŸ’” Hint
reversed() returns an iterator that goes backwards over the string.
ā“ Predict Output
advanced
1:30remaining
Output 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)
AError
Babcde
Cedcba
Daebcd
Attempts:
2 left
šŸ’” Hint
Each new character is added before the existing result string.
ā“ Predict Output
advanced
1:30remaining
Output 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)
Anohtyp
Bpython
CError
Dnothyp
Attempts:
2 left
šŸ’” Hint
Swapping characters from ends towards the center reverses the list.
🧠 Conceptual
expert
2:00remaining
Time complexity of different string reversal methods
Which string reversal approach has the best time complexity for reversing a string of length n?
AConverting string to list and swapping characters in place, O(n) time
BUsing reversed() and join(), which is O(n) due to join optimization
CUsing a loop that concatenates characters to a new string in each iteration, O(n²) time
DUsing slicing s[::-1] which creates a new reversed string in O(n) time
Attempts:
2 left
šŸ’” Hint
Consider how string concatenation inside loops affects time complexity.

Practice

(1/5)
1. Which of the following methods correctly reverses a string s in Python?
easy
A. Using reversed(s) without converting to string
B. Using slicing: s[::-1]
C. Using s.reverse() method
D. Using s.append() method

Solution

  1. Step 1: Understand string slicing

    The slicing syntax s[::-1] creates a new string with characters in reverse order.
  2. Step 2: Check other methods

    s.reverse() and s.append() are list methods, not valid for strings. reversed(s) returns an iterator, not a string, so it needs conversion.
  3. Final Answer:

    Using slicing: s[::-1] -> Option B
  4. Quick Check:

    String slicing with negative step reverses string [OK]
Hint: Use s[::-1] to reverse strings quickly [OK]
Common Mistakes:
  • Trying to use list methods on strings
  • Forgetting to convert reversed() iterator to string
  • Using append() on strings
2. Which of the following code snippets correctly reverses the string text = 'hello' and prints it?
easy
A. print(text.append('o'))
B. print(text.reverse())
C. print(text[::-2])
D. print(''.join(reversed(text)))

Solution

  1. Step 1: Analyze each option

    text.reverse() is invalid for strings. text[::-2] reverses with step 2, skipping characters. text.append() is invalid for strings.
  2. Step 2: Confirm correct syntax

    reversed(text) returns an iterator of characters reversed. ''.join() converts it back to string. This prints 'olleh'.
  3. Final Answer:

    print(''.join(reversed(text))) -> Option D
  4. Quick Check:

    reversed() + join() prints reversed string [OK]
Hint: Use ''.join(reversed(s)) to reverse strings [OK]
Common Mistakes:
  • Using reverse() method on strings
  • Using slicing with wrong step
  • Trying to append to strings
3. What is the output of the following code?
def reverse_string(s):
    result = ''
    for char in s:
        result = char + result
    return result

print(reverse_string('abc'))
medium
A. 'cba'
B. 'abc'
C. 'bac'
D. Error

Solution

  1. Step 1: Trace the loop for s='abc'

    Initially, result = ''. Iteration 1: char='a', result='a' + '' = 'a'. Iteration 2: char='b', result='b' + 'a' = 'ba'. Iteration 3: char='c', result='c' + 'ba' = 'cba'.
  2. Step 2: Return the reversed string

    The function returns 'cba', which is the reverse of 'abc'.
  3. Final Answer:

    'cba' -> Option A
  4. Quick Check:

    Prepending chars reverses string [OK]
Hint: Add each char before result to reverse string [OK]
Common Mistakes:
  • Appending char instead of prepending
  • Returning original string
  • Confusing order of concatenation
4. The following code is intended to reverse a string but has an error. What is the error?
def reverse_str(s):
    s.reverse()
    return s

print(reverse_str('hello'))
medium
A. Missing return statement
B. Function returns None
C. Strings do not have reverse() method
D. Syntax error in function definition

Solution

  1. Step 1: Check method usage on string

    The reverse() method exists for lists, not strings. Calling it on a string causes AttributeError.
  2. Step 2: Understand the error cause

    Since strings are immutable, they do not support reverse(). The code will raise an error at runtime.
  3. Final Answer:

    Strings do not have reverse() method -> Option C
  4. Quick Check:

    reverse() is list method, not string method [OK]
Hint: Use slicing or reversed() for strings, not reverse() [OK]
Common Mistakes:
  • Trying to use reverse() on strings
  • Expecting reverse() to modify string in place
  • Ignoring immutability of strings
5. You want to reverse only the vowels in a string s = 'hello world' while keeping other characters in place. Which approach below correctly achieves this?
hard
A. Extract vowels, reverse them, then replace vowels in original positions
B. Reverse entire string using s[::-1]
C. Use reversed(s) and join all characters
D. Replace vowels with '*' and then reverse string

Solution

  1. Step 1: Understand the problem

    We want to reverse only vowels, keeping consonants and spaces fixed in their places.
  2. Step 2: Analyze each option

    Reverse entire string using s[::-1] reverses whole string, changing all characters. Use reversed(s) and join all characters reverses all characters, not just vowels. Replace vowels with '*' and then reverse string replaces vowels with '*', losing original vowels. Extract vowels, reverse them, then replace vowels in original positions extracts vowels, reverses them, then puts them back in original vowel positions, preserving other characters.
  3. Final Answer:

    Extract vowels, reverse them, then replace vowels in original positions -> Option A
  4. Quick Check:

    Reverse vowels only by extracting and replacing [OK]
Hint: Extract vowels, reverse, then put back in place [OK]
Common Mistakes:
  • Reversing entire string instead of vowels only
  • Replacing vowels with placeholders without restoring
  • Ignoring character positions
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