Sort a K-Displaced Array
Updated
Problem statement
You are given an integer array nums and a non-negative integer k. In the fully sorted array, every input element appears at an index whose distance from its original index is at most k.
Return the values in nums sorted from smallest to largest. Equal values are indistinguishable.
Use the displacement guarantee to run faster than sorting all elements with a general O(n log n) comparison sort when k is much smaller than n.
Function
sortKDisplaced(nums: int[], k: int) → int[]Examples
Example 1
nums = [3,1,2,5,4]k = 2return = [1,2,3,4,5]Every value is at most two positions from its sorted location.
Example 2
nums = [1,2,3]k = 0return = [1,2,3]With displacement zero, the input is already sorted.
Example 3
nums = [2,1,2,1]k = 2return = [1,1,2,2]Duplicate values remain present.
Constraints
0 <= nums.length <= 200000.0 <= k <= max(0, nums.length - 1).-10^9 <= nums[i] <= 10^9.- The displacement guarantee is valid.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
- Meeting Rooms IIeBay · PHONE SCREENeBay interview questions
- Select the Top User MatchesDuolingo · PHONE SCREENDuolingo interview questions
- Meeting Rooms IIQuince · PHONE SCREENQuince interview questions
- Kth Largest Element in an ArrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Meeting Rooms IISecuronix · PHONE SCREEN
- Merge Multiple Sorted StreamsNVIDIA · PHONE SCREENNVIDIA interview questions