Reverse String Solution for Wipro

Company: Wipro · Difficulty: Easy · Topics: String, Two Pointers

Problem Statement

Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place with O(1) extra memory.

Key Understanding:

Examples

Example 1:

Input: s = ["h","e","l","l","o"]

Output: ["o","l","l","e","h"]

Explanation:

Example 2:

Input: s = ["H","a","n","n","a","h"]

Output: ["h","a","n","n","a","H"]

Explanation:

Example 3:

Input: s = ["a","b","c"]

Output: ["c","b","a"]

Explanation:

Intuition

The key insight is that we can reverse the string by swapping characters from both ends, moving towards the center. This approach works in-place without requiring extra memory.

Core Concept:

Why Two Pointers Work:

Approach

Approach 1: Two Pointers (Optimal)

Algorithm:

1. Initialize left pointer at start (index 0)

2. Initialize right pointer at end (index n-1)

3. While left < right:

Why This Works:

Complexity Analysis:

Approach 2: Using Extra Array

Algorithm:

1. Create a new array of same size as input

2. Copy characters from end to start of original array

3. Copy back to original array

Why This Works:

Complexity Analysis:

Approach 3: Recursive Approach

Algorithm:

1. Use recursion to swap characters

2. Base case: when left >= right (pointers meet or cross)

3. Recursive case: swap characters and call recursively with updated pointers

Why This Works:

Complexity Analysis:

Dry Run

Let's trace through Example 1: s = ["h","e","l","l","o"]

Two Pointers Approach:

Initial State: left=0, right=4, s = ["h","e","l","l","o"]

Iteration 1:

Iteration 2:

Iteration 3:

Final Result: s = ["o","l","l","e","h"]

Recursive Approach:

Call 1: reverseHelper(s, 0, 4)

Call 2: reverseHelper(s, 1, 3)

Call 3: reverseHelper(s, 2, 2)

Final Result: s = ["o","l","l","e","h"]

Key Insights

1. In-Place Operation: Two pointers approach is optimal for in-place reversal

2. Symmetric Swapping: Characters at symmetric positions need to be swapped

3. Center Point: For odd-length strings, the center character stays in place

4. Memory Efficiency: Two pointers use only O(1) extra space

5. Iterative vs Recursive: Iterative approach is more space-efficient

Edge Cases

1. Empty String: Array with 0 elements (no operation needed)

2. Single Character: Array with 1 element (no reversal needed)

3. Even Length: All characters get swapped

4. Odd Length: Center character stays in place, others get swapped

5. Palindrome: String reads the same forwards and backwards

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Two Pointers (Optimal)

Time Complexity: O(n) - We traverse half the array

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

Solution 2: Using Extra Array

Time Complexity: O(n) - We traverse the array twice

Space Complexity: O(n) - We need extra array

Advantages:

Disadvantages:

Solution 3: Recursive Approach

Time Complexity: O(n) - We make n/2 recursive calls

Space Complexity: O(n) - Due to recursion stack

Advantages:

Disadvantages:

More Wipro coding questions