Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Reverse an Array and Duplicate Elements
00:00
5 left

Reverse an Array and Duplicate Elements

EasyPython

Problem

Infor OS processes ordered configuration values represented as arrays. Given a mutable integer array nums and an index index, duplicate the element originally stored at index, then reverse the entire resulting array.

The operation must modify nums in place and return the same list object. The duplicated values must remain adjacent after reversal. In other words, first insert a copy immediately after the selected element, then reverse the complete array.

Formal Specification

Implement reverse_and_duplicate(nums, index):

  • Input: nums, a non-empty list of integers, and index, a valid zero-based index in the original list.
  • Output: The modified nums list after duplicating nums[index] and reversing the result.
  • The original relative order should be reversed, except that the selected value appears twice consecutively.
  • Use O(1) auxiliary space. The input list may grow by one element.

Constraints

  • 1 <= len(nums) <= 10^5
  • 0 <= index < len(nums)
  • -10^9 <= nums[i] <= 10^9

Function Signature

def reverse_and_duplicate(nums, index):
Interviewer

Your question is Reverse an Array and Duplicate Elements. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.