Implement Array.reverse

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Implement Array.reverse

Approach

Use two pointers starting at the beginning and end of the array, swapping elements in place and moving the pointers inward until they meet in the middle. This reverses the array with no extra memory allocation. Return this to allow method chaining.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Two Pointer Swap

Why It Works

Swapping elements from both ends toward the center reverses the array in n/2 swaps. Operating in-place modifies the original array, matching the native reverse behavior.

Updated Feb 2026