Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Efficient Sorting with Index

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

Your question is Efficient Sorting with Index. 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

NiCE CXone may need to rank interaction-quality scores before displaying the scores below a selected position. Given an integer array nums and a zero-based index k, sort nums in ascending order in place and return all elements whose sorted positions occur strictly after k.

Implement sort_and_return_after(nums, k) without using Python's built-in sorting functions. The algorithm must run in O(n log n) time and use O(1) auxiliary space, excluding the returned list.

Formal Specification

  • Input: nums, a mutable list of integers, and k, an integer index.
  • Output: A new list equal to the sorted array slice nums[k + 1:]. The input list must be sorted in place.
  • Duplicate values must remain present, and their relative ordering is irrelevant.

Constraints

  • 1 <= len(nums) <= 10^5
  • 0 <= k < len(nums)
  • -10^9 <= nums[i] <= 10^9
  • The input list must be sorted in place
  • Built-in sorting functions are not allowed

Function Signature

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