Problem 253749 · easy · Phase 02 Linear Data Structures

Reverse a Slice

arrays · slicing · reversal

Given a list nums and two indices i <= j, return a new list in which the elements from index i to index j (both inclusive) appear in reverse order while everything outside that range stays where it was.

Examples

Input:  nums = [1, 2, 3, 4, 5], i = 1, j = 3
Output: [1, 4, 3, 2, 5]
Explanation: the slice [2, 3, 4] becomes [4, 3, 2].

Input:  nums = [1, 2, 3], i = 0, j = 2
Output: [3, 2, 1]

Constraints

  • 1 <= len(nums) <= 10**5
  • 0 <= i <= j < len(nums)
  • Return a new list; do not modify nums.
  • Target: O(n) time.

Goals

  • Combine slices to rebuild a list around a changed region
  • Handle inclusive index ranges correctly
  • Keep the untouched prefix and suffix intact
Starting Python…