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

Reverse Characters in a String

Easy
Coding
ArraysStringsTwo Pointers

Problem

Problem

At Slack, you are asked to implement a utility function that reverses a string. Given a string s, return a new string with the characters of s in reverse order.

Formal Specification

  • Input: A string s
  • Output: A string containing the same characters as s, but in reverse order
  • Function behavior: Do not modify the meaning of characters; simply reverse their order

Examples

Example 1

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

Explanation: The characters h, e, l, l, o appear in reverse order as o, l, l, e, h.

Example 2

Input: s = "racecar"
Output: "racecar"

Explanation: This string is a palindrome, so reversing it produces the same string.

Example 3

Input: s = "a b!"
Output: "!b a"

Explanation: Spaces and punctuation are treated like normal characters and are reversed as well.

Constraints

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

Examples

Example 1
Inputs = "hello"Output"olleh"WhyThe characters are returned in reverse order.
Example 2
Inputs = "racecar"Output"racecar"WhyA palindrome reads the same forward and backward.
Example 3
Inputs = "a b!"Output"!b a"WhySpaces and punctuation are reversed along with letters.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain letters, digits, spaces, and punctuation
  • The solution should run in O(n) time

Function Signature

def reverse_string(s):

Problem

Problem

At Slack, you are asked to implement a utility function that reverses a string. Given a string s, return a new string with the characters of s in reverse order.

Formal Specification

  • Input: A string s
  • Output: A string containing the same characters as s, but in reverse order
  • Function behavior: Do not modify the meaning of characters; simply reverse their order

Examples

Example 1

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

Explanation: The characters h, e, l, l, o appear in reverse order as o, l, l, e, h.

Example 2

Input: s = "racecar"
Output: "racecar"

Explanation: This string is a palindrome, so reversing it produces the same string.

Example 3

Input: s = "a b!"
Output: "!b a"

Explanation: Spaces and punctuation are treated like normal characters and are reversed as well.

Constraints

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

Examples

Example 1
Inputs = "hello"Output"olleh"WhyThe characters are returned in reverse order.
Example 2
Inputs = "racecar"Output"racecar"WhyA palindrome reads the same forward and backward.
Example 3
Inputs = "a b!"Output"!b a"WhySpaces and punctuation are reversed along with letters.

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain letters, digits, spaces, and punctuation
  • The solution should run in O(n) time

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 StringEasyIDEOReverse Characters in a StringEasyPaycomReverse Characters in a StringEasy
Next question