Ctrl K

Flip the Characters in Place

Easy
You are given a list of characters chars. Put its elements in reverse order: the first character must end up last, the second one second to last, and so on. Return the flipped chars. Try to use only O(1) extra memory.
Example 1
chars=['r', 'a', 'd', 'a', 'r', 's'] → ['s', 'r', 'a', 'd', 'a', 'r']

After the flip the list is ['s', 'r', 'a', 'd', 'a', 'r'].

Example 2
chars=['Q', '?'] → ['?', 'Q']

The two characters just swap: ['?', 'Q'].

Example 3
chars=['z'] → ['z']

A single character has nowhere to go, so the list stays the same.

Constraints
  • 1 ≤ chars.length ≤ 10⁵
  • each chars[i] is a printable ASCII character
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWhere the Fragment Starts EasyMirror Phrase Check EasySame When Flipped EasyMirror the Vowels EasyHidden in Order Easy
loading Python…
Input
chars=['r', 'a', 'd', 'a', 'r', 's']
Expected
['s', 'r', 'a', 'd', 'a', 'r']

Run checks the first 3 tests in your browser. Submit checks all 35 tests on the server, hidden ones included.