Dataford
Interview Guides
Upgrade
All questions/Coding/Reverse Array In Place

Reverse Array In Place

Easy
Coding
Asked at 1 company1ArraysTwo Pointers
Also asked at
Michelin

Problem

Problem

At Spotify, you need to reverse a list of values without allocating another array. Given an array arr, modify it in place so its elements appear in reverse order.

Formal Specification

  • Input: A list of integers arr
  • Output: The same list after being reversed in place
  • Requirement: Do not create a second array for the result

Examples

Example 1

Input: arr = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]

Explanation: Swap the first and last elements, then continue moving inward.

Example 2

Input: arr = [7, 9]
Output: [9, 7]

Explanation: With two elements, a single swap reverses the array.

Example 3

Input: arr = [42]
Output: [42]

Explanation: A single-element array is already reversed.

Constraints

  • 0 <= len(arr) <= 10^5
  • -10^9 <= arr[i] <= 10^9
  • The array must be modified in place
  • Aim for linear time and constant extra space

A correct solution should handle empty arrays, odd-length arrays, and even-length arrays.

Examples

Example 1
Inputarr = [1, 2, 3, 4, 5]Output[5, 4, 3, 2, 1]WhySwap index pairs `(0,4)` and `(1,3)`. The middle value `3` remains unchanged.
Example 2
Inputarr = [7, 9]Output[9, 7]WhyThere is only one pair to swap, so the reversed array is produced in one operation.
Example 3
Inputarr = []Output[]WhyAn empty array has no elements to swap, so it remains unchanged.

Constraints

  • 0 <= len(arr) <= 10^5
  • -10^9 <= arr[i] <= 10^9
  • The array must be reversed in place
  • Do not allocate a second array for the final result

Function Signature

def reverse_array(arr):

Problem

Problem

At Spotify, you need to reverse a list of values without allocating another array. Given an array arr, modify it in place so its elements appear in reverse order.

Formal Specification

  • Input: A list of integers arr
  • Output: The same list after being reversed in place
  • Requirement: Do not create a second array for the result

Examples

Example 1

Input: arr = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]

Explanation: Swap the first and last elements, then continue moving inward.

Example 2

Input: arr = [7, 9]
Output: [9, 7]

Explanation: With two elements, a single swap reverses the array.

Example 3

Input: arr = [42]
Output: [42]

Explanation: A single-element array is already reversed.

Constraints

  • 0 <= len(arr) <= 10^5
  • -10^9 <= arr[i] <= 10^9
  • The array must be modified in place
  • Aim for linear time and constant extra space

A correct solution should handle empty arrays, odd-length arrays, and even-length arrays.

Examples

Example 1
Inputarr = [1, 2, 3, 4, 5]Output[5, 4, 3, 2, 1]WhySwap index pairs `(0,4)` and `(1,3)`. The middle value `3` remains unchanged.
Example 2
Inputarr = [7, 9]Output[9, 7]WhyThere is only one pair to swap, so the reversed array is produced in one operation.
Example 3
Inputarr = []Output[]WhyAn empty array has no elements to swap, so it remains unchanged.

Constraints

  • 0 <= len(arr) <= 10^5
  • -10^9 <= arr[i] <= 10^9
  • The array must be reversed in place
  • Do not allocate a second array for the final result

Function Signature

def reverse_array(arr):
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
MerckReverse Character Array In PlaceEasyOpenTableReverse Character Array In PlaceMediumImplement Bubble Sort AlgorithmMedium
Next question