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
Skillshare

Problem

Problem

At Apple, a text-processing utility needs a function that reverses a string. Given a string s, return a new string with its characters in reverse order.

Formal Specification

  • Input: a string s
  • Output: a string containing the characters of s in reverse order
  • Implement a function that preserves all characters exactly, including spaces, punctuation, and digits.

Examples

Example 1

Input: s = "swift"
Output: "tfiws"

Explanation: The characters are returned in reverse order.

Example 2

Input: s = "Hello, World!"
Output: "!dlroW ,olleH"

Explanation: Letters, punctuation, spaces, and symbols are all reversed as individual characters.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain uppercase letters, lowercase letters, digits, spaces, and punctuation
  • Return a new reversed string

A correct solution should run in linear time with respect to the length of the string.

Examples

Example 1
Inputs = "swift"Output"tfiws"WhyThe characters `s`, `w`, `i`, `f`, `t` are reversed to `t`, `f`, `i`, `w`, `s`.
Example 2
Inputs = "Hello, World!"Output"!dlroW ,olleH"WhyEvery character, including the comma, space, and exclamation mark, is reversed.
Example 3
Inputs = "a"Output"a"WhyA single-character string is unchanged when reversed.

Constraints

  • 0 <= len(s) <= 10^5
  • s consists of printable characters, including letters, digits, spaces, and punctuation
  • The function should return a new reversed string

Function Signature

def reverse_string(s):

Problem

Problem

At Apple, a text-processing utility needs a function that reverses a string. Given a string s, return a new string with its characters in reverse order.

Formal Specification

  • Input: a string s
  • Output: a string containing the characters of s in reverse order
  • Implement a function that preserves all characters exactly, including spaces, punctuation, and digits.

Examples

Example 1

Input: s = "swift"
Output: "tfiws"

Explanation: The characters are returned in reverse order.

Example 2

Input: s = "Hello, World!"
Output: "!dlroW ,olleH"

Explanation: Letters, punctuation, spaces, and symbols are all reversed as individual characters.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain uppercase letters, lowercase letters, digits, spaces, and punctuation
  • Return a new reversed string

A correct solution should run in linear time with respect to the length of the string.

Examples

Example 1
Inputs = "swift"Output"tfiws"WhyThe characters `s`, `w`, `i`, `f`, `t` are reversed to `t`, `f`, `i`, `w`, `s`.
Example 2
Inputs = "Hello, World!"Output"!dlroW ,olleH"WhyEvery character, including the comma, space, and exclamation mark, is reversed.
Example 3
Inputs = "a"Output"a"WhyA single-character string is unchanged when reversed.

Constraints

  • 0 <= len(s) <= 10^5
  • s consists of printable characters, including letters, digits, spaces, and punctuation
  • The function should 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
Reverse Characters in a StringEasyEverlight SolarReverse Characters in a StringEasyPaycomReverse Characters in a StringEasy
Next question