Workspace Reverse Array
0d
DSA Problem Sheet

Reverse an Array

Return a reversed copy by exchanging symmetric positions.

EXECUTION LAB

Return a reversed copy by exchanging symmetric positions.

Input and constraints
  • Use at most 32 integers. Each numeric input is between -1000 and 1000.

Code inspector

def solve(values, target):
a = values.copy()
left = 0
right = (len(a) - 1)
while (left < right):
a[left], a[right] = a[right], a[left]
left = (left + 1)
right = (right - 1)
return a

Visualizer

Step 1 of 3
03left
18
21
35right

Inspect the current state

Exchange symmetric positions 0 and 3.

Variable watch

left 0right 3

Step explanation

Exchange symmetric positions 0 and 3.

comparisons 1reads 0writes 0swaps 0recursiveCalls 0
Educational simulation · O(n) time · O(n) returned copy, O(1) auxiliary space

Worked explanation

Two friends rehang a row of photos, swapping the outermost unfinished pair.

Guided solution

Every successful swap finalizes both ends of the remaining interval.

O(n) time · O(n) returned copy, O(1) auxiliary auxiliary space

Examples

Worked example · {"values":[3,8,1,5],"target":0} ⇒ [5,1,8,3]

Edge case 1 · {"values":[],"target":0} ⇒ []

Edge case 2 · {"values":[4],"target":0} ⇒ [4]

Progressive hints

Hint 1Two friends rehang a row of photos, swapping the outermost unfinished pair.
Hint 2Every successful swap finalizes both ends of the remaining interval.
Hint 3Step through the worked example and inspect the highlighted variables before each update.

Common mistake

Swapping across the entire index range undoes earlier exchanges.

Interview follow-up

How many swaps does a length-n array require?