Magicsheet logo

Reverse Vowels of a String

Easy
55.3%
Updated 6/1/2025

Reverse Vowels of a String

What is this problem about?

The Reverse Vowels of a String interview question asks you to reverse only the vowels (a, e, i, o, u — both lowercase and uppercase) in a given string, while keeping all other characters in their original positions. The result is the same string with vowels in reversed order and consonants and non-letter characters untouched.

Why is this asked in interviews?

This problem is asked at Apple, Uber, Microsoft, Meta, Amazon, Oracle, Google, and Bloomberg as a clean application of the two-pointer technique with a character-type filter. It is the "reverse only specific characters" pattern — a variant of the basic reverse that appears in text transformation, data anonymization, and encoding systems. It tests whether candidates can apply two-pointer logic with a conditional skip.

Algorithmic pattern used

The pattern is the two-pointer technique with vowel filtering. Convert the string to a list. Place left at 0 and right at the last index. While left < right: advance left until it points to a vowel, advance right until it points to a vowel, then swap and move both pointers inward. Use a vowel set {'a','e','i','o','u','A','E','I','O','U'} for O(1) lookup. Rejoin and return.

Example explanation

Input: "hEllow"

Vowels in order: E (index 1), o (index 4). Two pointers:

  • left=0 ('h' — not vowel → advance). left=1 ('E' — vowel).
  • right=5 ('w' — not vowel → retreat). right=4 ('o' — vowel).
  • Swap 'E' and 'o'. String: "holloEw" → wait, let's retrace: "hEllow".
    • After swap at indices 1 and 4: "hollEw" — 'h','o','l','l','E','w'.

Result: "hollEw".

Wait — "hEllow" → vowels are 'E'(1) and 'o'(4). Swap → "hollEw". Correct.

Common mistakes candidates make

  • Forgetting uppercase vowels — the vowel set must include both cases.
  • Not advancing both pointers after a swap, causing an infinite loop.
  • Using string indexing without a mutable copy — convert to a list first.
  • Off-by-one when the left pointer reaches or crosses right.

Interview preparation tip

For the Reverse Vowels of a String coding problem, the two-pointer string interview pattern is the approach. Build a vowel set first for fast O(1) lookup. Interviewers at Nvidia and USAA often ask follow-ups like "what if you reverse only consonants?" — the same two-pointer pattern applies with an isalpha() and not vowel condition. Demonstrating this generalization shows pattern fluency.

Similar Questions