Reverse a string in linear time with O(1) extra space

Quick Overview

Write a function that returns a string with its characters in reverse order, in linear time and with constant extra space beyond the result. It checks how well a candidate understands Python's immutable strings and whether their space analysis is accurate.

Reverse a string in linear time with O(1) extra space

Company: Goldman Sachs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Write a function that takes a string `s` and returns it with its characters in reverse order. The function should run in $O(n)$ time and use $O(1)$ extra space, not counting the space needed to store the result. Reverse the characters in place where the language allows it; otherwise, use no more extra space than necessary. ### Function Signature ```python def reverse_string(s: str) -> str: ``` ### Rules - Every element of the Python string is one character, including spaces and punctuation. The character at index `i` of the output is `s[len(s) - 1 - i]`. - The output has the same length as `s`, and every character keeps its case. ### Constraints - `1 <= len(s) <= 10^4` ### Examples ```text Input: s = "hello" Output: "olleh" ``` Reading "hello" from the last character to the first gives o, l, l, e, h. ```text Input: s = "Python" Output: "nohtyP" ``` The capital P moves from the front to the end and stays uppercase. ```text Input: s = "race car" Output: "rac ecar" ``` The space counts as a character. It sits at index 4 of the input and ends up at index 3 of the output, since 8 - 1 - 4 = 3.

Overview: Write a function that returns a string with its characters in reverse order, in linear time and with constant extra space beyond the result. It checks how well a candidate understands Python's immutable strings and whether their space analysis is accurate.

Goldman Sachs logo
Goldman Sachs
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Write a function that takes a string s and returns it with its characters in reverse order. The function should run in O(n)O(n) time and use O(1)O(1) extra space, not counting the space needed to store the result. Reverse the characters in place where the language allows it; otherwise, use no more extra space than necessary.

Function Signature

def reverse_string(s: str) -> str:

Rules

  • Every element of the Python string is one character, including spaces and punctuation. The character at index i of the output is s[len(s) - 1 - i] .
  • The output has the same length as s , and every character keeps its case.

Constraints

  • 1 <= len(s) <= 10^4

Examples

Input: s = "hello"
Output: "olleh"

Reading "hello" from the last character to the first gives o, l, l, e, h.

Input: s = "Python"
Output: "nohtyP"

The capital P moves from the front to the end and stays uppercase.

Input: s = "race car"
Output: "rac ecar"

The space counts as a character. It sits at index 4 of the input and ends up at index 3 of the output, since 8 - 1 - 4 = 3.