Interleave Array Halves
Updated
Problem statement
You are given an integer array nums. Rearrange it so that its first half occupies the even-indexed positions and its second half, in reverse order, occupies the odd-indexed positions.
For this exercise, assume the array length is even and indices start at 0. Preserve the order of the first half and every occurrence of a duplicate value. Return the complete rearranged array without changing nums.
Function
interleaveHalves(nums: int[]) → int[]Examples
Example 1
nums = [1,2,3,4,5,6]return = [1,6,2,5,3,4]The first half [1,2,3] goes to indices 0, 2, and 4. The reversed second half [6,5,4] fills indices 1, 3, and 5.
Example 2
nums = [2,2,-3,9]return = [2,9,2,-3]The two occurrences of 2 stay in their first-half order. The reversed second half is [9,-3].
Constraints
- 2 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
- The array length is even.
More Autodesk interview questions
Similar questions at other companies
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Minimum Index Distance Between Person and CakeSnowflake · OA · PHONE SCREENSnowflake interview questions
- Meandering ArrayVerisk · OA
- Collect Branches for a Bird NestVisa · OAVisa interview questions
- Maximum Requests in a Time WindowIBM · OAIBM interview questions
- Set IntersectionBlackRock · OABlackRock interview questions
- Can Every Package Fit in a BoxIMC Trading · OAIMC Trading interview questions
- Filter Odds and Reverse EvensUpstart · OAUpstart interview questions
- Score Difference When Even Picks Reverse the ListMicrosoft · OAMicrosoft interview questions
- Maximum Events in a 60-Second WindowMarshall Wace · OAMarshall Wace interview questions