FastPrepReverse a String with Two Pointers

Reverse a String with Two Pointers

Snowflake logoSnowflake● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a string s, return its characters in reverse order. Preserve every character exactly, including spaces, punctuation, and letter case.

Use two pointers that move inward from opposite ends. You may copy the input to a mutable character array, swap the two pointed-to characters, and return the resulting string. The empty string returns the empty string.

Function

reverseString(s: String) → String

Examples

Example 1

s = "hello"return = "olleh"

Swap h with o and e with l; the middle l stays in place.

Example 2

s = "A b!"return = "!b A"

Spaces and punctuation reverse just like letters; case is unchanged.

Constraints

  • 0 <= s.length <= 10000.
  • Every character is printable ASCII, with character code from 32 through 126.

More Snowflake problems

See Snowflake hiring insights
Saved
public String reverseString(String s) {
    // Write your solution here.
}
s"hello"
expected"olleh"
Checking account…