Back to Practice Dashboard
Python BasicsEasy
Reverse an Array
Learn how to solve the 'Reverse an Array' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function reverse_array(arr) that takes a list of integers arr and returns a new list with the elements in reverse order. For example, [1, 2, 3] becomes [3, 2, 1].
Constraints
- •0 <= len(arr) <= 10^5
- •-10^9 <= arr[i] <= 10^9
Examples
Example 1
Input
arr = [1, 2, 3, 4, 5]
Output
[5, 4, 3, 2, 1]
Explanation
The elements are reversed: first becomes last and last becomes first.
Example 2
Input
arr = [10, 20]
Output
[20, 10]
Explanation
Two elements swapped.
Example 3
Input
arr = [7]
Output
[7]
Explanation
Single element array reversed is the same.
Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.