A typical programming interview question is “reverse a string, in place”. if you understand pointers, the solution is simple. even if you don’t, it can be accomplished using array indices. i usually ask candidates this question first, so they get the algorithm in their head. then i play dirty by asking them to reverse the string word by word, in place. for example if our string is “the house is blue”, the return value would be “blue is house the”. the words are reversed, but the letters are still in order (within the word).
Solution
Solving the initial problem of just reversing a string can either be a huge help or a frustrating hinderance. most likely the first attempt will be to solve it the same way, by swapping letters at the front of the string with letters at the back, and then adding some logic to keep the words in order. this attempt will lead to confusion pretty quickly.
for example, if we start by figuring out that “the” is 3 letters long and then try to put the “t” from “the” where the “l” from “blue” is, we encounter a problem. where do we put the “l” from “blue”? hmm… well we could have also figured out how long “blue” was and that would tell us where to put the “l” at… but the “e” from “blue” needs to go into the space after “the”. argh. its getting quite confusing. in fact, i would be delighted to even see a solution to this problem using this attack method. i don’t think its impossible, but i think it is so complex that it’s not worth pursuing.
here’s a hint. remember before when we just reversed “the house is blue”? what happened?
initial: the house is blue
reverse: eulb si esuoh eht
look at the result for a minute. notice anything? if you still don’t see it, try this.
initial: the house is blue
reverse: eulb si esuoh eht
wanted : blue is house the
the solution can be attained by first reversing the string normally, and then just reversing each word.
2026 Update: String Reversal — Deceptively Deep Interview Topic
Reversing a string seems trivial, but interviewers progressively add constraints to find the depth of your knowledge:
# Level 1: Basic reversal
def reverse_basic(s: str) -> str:
return s[::-1]
# Level 2: In-place (requires mutable, e.g., list)
def reverse_in_place(chars: list) -> None:
left, right = 0, len(chars) - 1
while left str:
return ' '.join(s.split()[::-1])
# Level 4: Reverse words without extra space (two-pass)
def reverse_words_in_place(s: str) -> str:
chars = list(s.strip())
# Pass 1: reverse entire string
reverse_in_place(chars)
# Pass 2: reverse each word
start = 0
for end in range(len(chars) + 1):
if end == len(chars) or chars[end] == ' ':
reverse_in_place(chars[start:end])
start = end + 1
return ''.join(chars)
# Level 5: Unicode-aware reversal (grapheme clusters)
# "café" = c-a-f-e-combining_acute — naive reversal breaks it
def reverse_unicode_safe(s: str) -> str:
import unicodedata
# Use grapheme library in production; here a simplified approach
# For ASCII + common unicode, this works:
return s[::-1] # Python handles most cases correctly natively
# Test cases
print(reverse_words("the sky is blue")) # "blue is sky the"
print(reverse_words_in_place(" hello world ")) # "world hello"
Follow-up questions asked in 2025-2026 interviews:
- “What if the string contains emojis?” (grapheme cluster awareness —
"👨👩👧"is one grapheme, multiple code points) - “Reverse a linked list instead” (same two-pointer logic, different data structure)
- “What’s the time/space complexity of each approach?” (slicing = O(n) space; in-place = O(1))
- “How would you reverse a stream of characters with finite memory?” (buffer-based approach)
💡Strategies for Solving This Problem
Problem Analysis
String reversal is a fundamental operation with multiple approaches depending on the language and constraints.
Key Approaches:
- Built-in Methods: Most languages provide simple ways to reverse strings
- Two-Pointer Technique: Swap characters from both ends moving toward the center
- Stack-Based: Push all characters onto a stack, then pop them off
- Recursive: Reverse the substring and append the first character
Important Considerations:
- In-place vs. New String: Some languages have immutable strings (JavaScript, Python), others allow in-place modification (C, C++)
- Unicode Handling: Be careful with multi-byte characters and emojis
- Performance: Two-pointer is generally most efficient for mutable strings
Follow-up Questions to Expect:
- Can you do it in-place without extra space?
- How would you reverse only words in a sentence?
- What about reversing words AND the sentence?