Reverse a Character Array In Place with Constant Extra Memory
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Reverse a string given as an array of its characters. The reversal must happen in place: rearrange the elements of the given list itself, using only a constant amount of extra memory, and return that list.
### Function Signature
```python
def reverse_characters(chars: list[str]) -> list[str]:
```
### Rules
- Modify `chars` itself. Do not build a reversed copy, and do not call a built-in reverse routine or use slicing that creates a new list.
- Return the same list object after the reversal.
### Constraints
- `1 <= len(chars) <= 10^5`
- every element is a single printable ASCII character (codes 32 to 126); spaces and punctuation are ordinary characters
### Examples
**Example 1**
```text
Input: chars = ["h", "e", "l", "l", "o"]
Output: ["o", "l", "l", "e", "h"]
```
**Example 2**
```text
Input: chars = ["a", " ", "b", "!"]
Output: ["!", "b", " ", "a"]
```
With an even length, every element moves; the space is reversed like any other character.
**Example 3**
```text
Input: chars = ["z"]
Output: ["z"]
```
Overview: Reverse a string given as an array of characters in place, using only constant extra memory, and return the same list. A short coding warm-up that tests index handling, swapping elements and odd and even lengths without building a copy.
You are given a string as a list `chars` of its characters. Reverse the string in place: rearrange the elements of the given list itself, using only a constant amount of extra memory, and return that list.
### Rules
- Modify `chars` itself. Do not build a reversed copy, and do not call a built-in reverse routine or use slicing that creates a new list.
- Return the same list object after the reversal.
The returned list is graded: with `n = len(chars)`, its element at index `i` must be the original element at index `n - 1 - i`. Spaces and punctuation are ordinary characters and are reversed like any other element.
### Constraints
- `1 <= len(chars) <= 10^5`
- every element is a single printable ASCII character (codes 32 to 126); spaces and punctuation are ordinary characters
### Example 1
```text
Input: chars = ["h", "e", "l", "l", "o"]
Output: ["o", "l", "l", "e", "h"]
```
### Example 2
```text
Input: chars = ["a", " ", "b", "!"]
Output: ["!", "b", " ", "a"]
```
With an even length, every element moves; the space is reversed like any other character.
Constraints
- 1 <= len(chars) <= 10^5
- every element is a single printable ASCII character (codes 32 to 126); spaces and punctuation are ordinary characters
Examples
Input: (['h', 'e', 'l', 'l', 'o'],)
Expected Output: ['o', 'l', 'l', 'e', 'h']
Explanation: Source example 1: odd length 5, so the middle 'l' stays in place.
Input: (['a', ' ', 'b', '!'],)
Expected Output: ['!', 'b', ' ', 'a']
Explanation: Source example 2: even length 4, every element moves and the space is reversed like any other character.
Hints
- Where must the first element end up, and which element has to take its place?
- With an odd length one element never moves; with an even length every element moves. What does that tell you about which positions exchange values?
- Make sure each exchange happens exactly once: doing it a second time undoes it.