Reverse a list in-place
Company: Microsoft
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to implement an in-place list reversal and the underlying competencies in array/list manipulation, index/pointer management, and analysis of space and time complexity.
Constraints
- 0 <= len(arr) <= 10^5
- Elements fit in a 32-bit signed integer
- Must reverse in-place: O(1) extra space
- No slicing, list(), or reversed() (Python); no built-in reverse helpers in other languages
Examples
Input: ([1, 4, 8, 12, 16, 20],)
Expected Output: [20, 16, 12, 8, 4, 1]
Explanation: Given even-length example reversed.
Input: ([],)
Expected Output: []
Explanation: Empty list stays empty; loop never runs.
Input: ([1],)
Expected Output: [1]
Explanation: Single element is unchanged.
Input: ([9, 5, 1],)
Expected Output: [1, 5, 9]
Explanation: Odd length: ends swap, middle element (5) stays put.
Input: ([0, 2, 4, 6],)
Expected Output: [6, 4, 2, 0]
Explanation: Even length, all pairs swapped.
Input: ([-3, -1, 0, 7, -2],)
Expected Output: [-2, 7, 0, -1, -3]
Explanation: Negatives and a zero are handled like any other value.
Input: ([5, 5, 5, 5],)
Expected Output: [5, 5, 5, 5]
Explanation: Duplicates: reversed order is identical to the input.
Hints
- Use two pointers: one at the start, one at the end of the list.
- Swap the elements the two pointers reference, then move both pointers toward the middle.
- Stop when the left pointer meets or crosses the right pointer. For odd-length lists the middle element stays in place, which is correct.