Reverse Vowels of a String

Algorithms
Easy
Stripe
91K views

Given a string `s`, reverse only all the vowels in the string and return it. The vowels are 'a', 'e', 'i', 'o', and 'u'. Use the Two-Pointer technique.

Why Interviewers Ask This

Stripe values engineers who write clean, efficient code that handles edge cases gracefully. This question tests your ability to manipulate strings in-place using the Two-Pointer technique, a core skill for optimizing memory usage. It evaluates whether you can identify patterns, handle case sensitivity correctly, and implement an O(n) solution without allocating extra space.

How to Answer This Question

1. Clarify Requirements: Immediately confirm if 'vowels' include uppercase letters (e.g., 'A', 'E') and if the input string is mutable or immutable. Mentioning Stripe's focus on robustness here is key. 2. Define Strategy: Propose the Two-Pointer approach. Explain that one pointer starts at the beginning (left) and another at the end (right), moving inward until they meet. 3. Implement Logic: Detail the inner loop where pointers skip non-vowel characters. Once both point to vowels, swap them and move both pointers inward. 4. Handle Edge Cases: Explicitly mention handling empty strings, single characters, or strings with no vowels to demonstrate thoroughness. 5. Analyze Complexity: Conclude by stating the time complexity is O(n) since each character is visited once, and space complexity is O(1) if modifying a character array in place.

Key Points to Cover

  • Explicitly define what constitutes a vowel (case sensitivity) before coding
  • Demonstrate understanding of immutability and the need for a character array
  • Explain the logic for skipping non-vowel characters to optimize the traversal
  • Confirm the solution achieves O(n) time complexity and O(1) space complexity
  • Show awareness of edge cases like empty strings or strings with no vowels

Sample Answer

To solve this efficiently while respecting Stripe's emphasis on performance, I would use the Two-Pointer technique. First, I need to clarify if the vowels are case-sensitive. Assuming standard behavior where 'a' and 'A' are both vowels, I will convert the input string into a character array because strings in many languages are immutable. I initialize two pointers: left at index 0 and right at the length minus one. I then enter a loop that continues as long as left is less than right. Inside the loop, I check if the character at the left pointer is a vowel. If it isn't, I increment left and continue. Similarly, if the character at the right pointer isn't a vowel, I decrement right. Once both pointers land on vowels, I swap the characters at these positions. After the swap, I move both pointers inward—incrementing left and decrementing right—and repeat the process. Finally, I convert the modified character array back into a string and return it. For example, given 'hello', the pointers find 'e' and 'o', swap them to get 'holle'. This approach ensures we only traverse the string once, achieving O(n) time complexity and O(1) auxiliary space, which aligns perfectly with systems requiring high throughput.

Common Mistakes to Avoid

  • Failing to account for uppercase vowels, leading to incorrect swaps or missed characters
  • Using a stack or regex instead of Two-Pointers, resulting in unnecessary O(n) space usage
  • Not converting the string to a mutable array first, causing runtime errors when attempting swaps
  • Incorrectly updating pointers after a swap, potentially swapping the same pair multiple times

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 57 Stripe questions