FastPrepCorrect an In-Place Array Reversal

Correct an In-Place Array Reversal

Duolingo logoDuolingo● EasyINTERNPHONE SCREEN
Learn

Problem statement

A proposed reversal loops over every index and swaps each element with its mirror. That swaps every pair twice and restores the original array.

Given nums, reverse it in place by swapping each mirrored pair exactly once, then return the same array.

Function

reverseArray(nums: int[]) → int[]

Examples

Example 1

nums = [1,2,3,4]return = [4,3,2,1]

Swap indices 0 and 3, then 1 and 2.

Example 2

nums = [7]return = [7]

A one-element array is already reversed.

Constraints

  • 0 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • Use O(1) extra space.

More Duolingo problems

See Duolingo hiring insights
public int[] reverseArray(int[] nums) {
    // Write your code here.
}
nums[1,2,3,4]
expected[4,3,2,1]
Checking account…