Quick 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.

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

  1. Where must the first element end up, and which element has to take its place?
  2. 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?
  3. Make sure each exchange happens exactly once: doing it a second time undoes it.

Loading coding console...

Show the approach

Approach

Use two indices: i starts at index 0 and j at index n - 1. While i < j, swap chars[i] and chars[j], then move i one step right and j one step left. Invariant: before each check, every index below i and every index above j already holds its final value (the original element from the mirrored index n - 1 - k), and the indices from i to j are untouched. When i >= j, the untouched region is either empty (even length) or the single middle index of an odd-length list, whose element is already in its final place, so the whole list is reversed. Each mirrored pair is swapped exactly once. That matters: a loop over the full length swaps every pair twice and returns the input unchanged, and a loop that stops one step early leaves the central pair unswapped. Only two indices and one temporary are used, so the extra memory is constant and the list itself is returned. Edge cases: a singleton list (the loop never runs), lists of one repeated character or palindromes (the output equals the input), and space, tilde, quote and backslash characters, which are ordinary elements.

Time complexity:
O(n)
Space complexity:
O(1)