Dataford
Interview Guides
Upgrade
All questions/Coding/Reverse Characters in a String

Reverse Characters in a String

Easy
Coding
Asked at 1 company1ArraysStringsRecursion
Also asked at
IDEO

Problem

Problem

At Notion, a text utility needs a function that returns a string with its characters reversed. Given a string s, write a function that returns a new string containing the characters of s in reverse order.

Formal Specification

  • Input: a string s
  • Output: a string representing s reversed
  • The function should preserve all characters exactly, including spaces, punctuation, digits, and case.

Examples

Example 1

  • Input: s = "hello"
  • Output: "olleh"
  • Explanation: The characters are returned from last to first.

Example 2

  • Input: s = "Data 123"
  • Output: "321 ataD"
  • Explanation: Letters, digits, and spaces are all reversed as characters.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain uppercase letters, lowercase letters, digits, spaces, punctuation, and Unicode characters

Notes

You may assume the input is a valid string. Aim for a solution that runs in linear time. If you choose to build the result manually, avoid repeatedly concatenating immutable strings inside a loop, since that can be less efficient for large inputs.

Examples

Example 1
Inputs = "hello"Output"olleh"WhyReading the characters from the end to the beginning gives the reversed string.
Example 2
Inputs = "Data 123"Output"321 ataD"WhyThe space is treated like any other character, so the entire sequence is reversed.
Example 3
Inputs = "a"Output"a"WhyA single-character string is already the same when reversed.

Constraints

  • 0 <= len(s) <= 10^5
  • s consists of valid string characters, including spaces and punctuation
  • Return a new reversed string

Function Signature

def reverse_string(s):

Problem

Problem

At Notion, a text utility needs a function that returns a string with its characters reversed. Given a string s, write a function that returns a new string containing the characters of s in reverse order.

Formal Specification

  • Input: a string s
  • Output: a string representing s reversed
  • The function should preserve all characters exactly, including spaces, punctuation, digits, and case.

Examples

Example 1

  • Input: s = "hello"
  • Output: "olleh"
  • Explanation: The characters are returned from last to first.

Example 2

  • Input: s = "Data 123"
  • Output: "321 ataD"
  • Explanation: Letters, digits, and spaces are all reversed as characters.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain uppercase letters, lowercase letters, digits, spaces, punctuation, and Unicode characters

Notes

You may assume the input is a valid string. Aim for a solution that runs in linear time. If you choose to build the result manually, avoid repeatedly concatenating immutable strings inside a loop, since that can be less efficient for large inputs.

Examples

Example 1
Inputs = "hello"Output"olleh"WhyReading the characters from the end to the beginning gives the reversed string.
Example 2
Inputs = "Data 123"Output"321 ataD"WhyThe space is treated like any other character, so the entire sequence is reversed.
Example 3
Inputs = "a"Output"a"WhyA single-character string is already the same when reversed.

Constraints

  • 0 <= len(s) <= 10^5
  • s consists of valid string characters, including spaces and punctuation
  • Return a new reversed string

Function Signature

def reverse_string(s):
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
CReverse Characters in a StringEasyReverse Characters in a StringEasyReverse Characters in a StringEasy
Next question