Reverse String
Updated
Problem statement
You are given a string as an array of characters s. Reverse the order of the characters in place and return the same array.
Change the input array itself and use only O(1) extra memory: do not build a second array or string whose size grows with the input.
Function
reverseString(s: char[]) → char[]Examples
Example 1
s = ["Z","i","l","l","o","w"]return = ["w","o","l","l","i","Z"]Swap the first and last characters, then the second and second-to-last, then the third and third-to-last: Z with w, i with o, and l with l.
Example 2
s = ["a","b","c","d","e"]return = ["e","d","c","b","a"]The length is odd, so the middle character c has no partner and stays where it is. Only a with e and b with d are swapped.
Example 3
s = ["Q"]return = ["Q"]A single character is its own reverse; there is nothing to swap.
Example 4
s = ["H","i",","," ","4","2","!"]return = ["!","2","4"," ",",","i","H"]Spaces, digits and punctuation are characters like any other. The space is the middle character, so it stays where it is, and the ! at the end becomes the first character.
Constraints
1 <= s.length <= 105- Each
s[i]is a printable ASCII character (character codes32to126), so it can be a letter, a digit, a space or a symbol.
More Zillow Phone Screen questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
- Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Jump GamePHONE SCREEN · Seen Dec 2022
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- Two Sum: All PairsPHONE SCREEN · Seen Nov 2022
- Employee Free TimePHONE SCREEN · Seen Oct 2022
- First Unique Character in a StringPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- Run-Length Encoding and DecodingAirbnb · PHONE SCREENAirbnb interview questions
- Check if a String Is a PalindromeCisco · PHONE SCREENCisco interview questions
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Reverse a StringAmazon · PHONE SCREEN
- Reverse a String ManuallyNewgen Software · PHONE SCREEN
- Reverse a String with Two PointersSnowflake · PHONE SCREENSnowflake interview questions
- Palindrome with One DeletionWhatNot · PHONE SCREENWhatNot interview questions
- Valid Word AbbreviationMeta · PHONE SCREENMeta interview questions
- Rearrange String by Alternating EndsHudson River Trading · OAHudson River Trading interview questions
- Alternate String EndsCapital One · OACapital One interview questions