Quick Overview

A warm-up coding task that asks for the characters of a printable ASCII string in reverse order, with spaces and punctuation moved like any other character. It checks basic index handling and edge cases such as the empty string and very long inputs.

Reverse the Characters of an ASCII String

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Write a function that returns a string with its characters in reverse order. The report names this warm-up only as reversing a string. This version fixes the input to printable ASCII, so that reversing characters and reversing bytes give the same result, and asks for the reversed string as the return value. ### Function Signature ```python def reverse_string(s: str) -> str: ``` ### Rules - Return a string of the same length whose character at index `i` is `s[len(s) - 1 - i]`. - Every character moves, including spaces and punctuation; nothing is trimmed, collapsed or changed in case. - An empty input returns an empty string. ### Constraints - `0 <= len(s) <= 10^5` - `s` contains only printable ASCII characters (codes 32 to 126). ### Examples **Example 1** ```text Input: s = "firmware" Output: "erawmrif" ``` **Example 2** ```text Input: s = "a b!" Output: "!b a" ``` The space and the exclamation mark move like any other character. **Example 3** ```text Input: s = "" Output: "" ```

Overview: A warm-up coding task that asks for the characters of a printable ASCII string in reverse order, with spaces and punctuation moved like any other character. It checks basic index handling and edge cases such as the empty string and very long inputs.

Write a function `reverse_string(s)` that returns the string `s` with its characters in reverse order. The input is restricted to printable ASCII, so reversing characters and reversing bytes give the same result. Return the reversed string as the function's return value. ### Rules - Return a string of the same length whose character at index `i` is `s[len(s) - 1 - i]`. - Every character moves, including spaces and punctuation; nothing is trimmed, collapsed or changed in case. - An empty input returns an empty string. ### Constraints - `0 <= len(s) <= 10^5` - `s` contains only printable ASCII characters (codes 32 to 126). - No value in this problem exceeds 2^31 - 1. ### Examples **Example 1** ```text Input: s = "firmware" Output: "erawmrif" ``` **Example 2** ```text Input: s = "a b!" Output: "!b a" ``` The space and the exclamation mark move like any other character.

Constraints

  • 0 <= len(s) <= 10^5
  • s contains only printable ASCII characters (codes 32 to 126)

Examples

Input: ('',)

Expected Output: ''

Explanation: Minimum valid: an empty string returns an empty string.

Input: ('Q',)

Expected Output: 'Q'

Explanation: Minimum valid: a single character returns itself.

Hints

  1. For each output position i, work out which input position supplies its character.
  2. Spaces, punctuation, quotes and backslashes are ordinary characters here: nothing is trimmed, collapsed or re-cased.
  3. Check your approach on lengths 0, 1, 2 and 3 before trusting it on longer strings.

Loading coding console...

Show the approach

Approach

Algorithm: build an output of the same length n and fill position i with s[n - 1 - i], i.e. read the input from its last index back to its first. Invariant: after positions 0..k-1 are filled they hold s[n-1], s[n-2], ..., s[n-k], exactly the first k characters the rule requires. Correctness: every output index i in 0..n-1 is written once with the character the rule names, and i -> n-1-i is a bijection on 0..n-1, so the result has length n and uses each input character exactly once. Edge cases: an empty string gives an empty result; a single character maps to itself; for odd n the middle index maps to itself, for even n there is no fixed middle; spaces, punctuation, quotes and backslashes are ordinary characters and case is never changed. Because the input is printable ASCII, every character is one byte and one UTF-16 code unit, so per-character reversal agrees in Python, JavaScript, Java and C++.

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