Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse Linked List or Sort Efficiently

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Reverse Linked List or Sort Efficiently. Start with the requirements on the right.

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.

Problem

Bosch ESI[tronic] can represent diagnostic event identifiers as an ordered Python list. Given the list and two inclusive indices, reverse only the elements between those indices in place, then return the modified list.

Do not create a second list proportional to the reversed range. The elements outside the selected range must remain unchanged.

Formal Specification

Implement reverse_range(nums, left, right).

  • Input: nums, a list of integers; left and right, inclusive zero-based indices.
  • Output: The same list object after reversing nums[left:right + 1].
  • The function must use constant auxiliary space.

Constraints

  • 1 <= len(nums) <= 10^5
  • 0 <= left <= right < len(nums)
  • -10^9 <= nums[i] <= 10^9
  • The reversal must use O(1) auxiliary space

Function Signature

def reverse_range(nums, left, right):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output