Reported November 2025
Googleheap priority queue

Lexicographically Smallest Array with K-Limited Right Moves

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

With up to 200000 elements, trying permutations or even an O(n^2) scan for each position dies fast on this Google question, reported in November 2025. It's a greedy build of the output with a heap, and the catch is a hidden deadline on every element. If your OA is a day or two out, learn the forced-placement check and you're most of the way there. The statement reads like a permutation puzzle, but it's really a sliding window of eligible indices plus one mandatory pick when a deadline hits. If you blank when the deadline logic gets fuzzy, StealthCoder runs invisibly during the live assessment and can hand you the heap structure. Still, the whole idea fits in about ten lines.

The problem

Reorder nums into a permutation of its elements. An element originally at index j may move to any earlier index, but if it moves to a later index i, it must satisfy i - j <= k.
Return the lexicographically smallest reachable array. Equal values are treated as distinct occurrences; when two equal values are both eligible for the same output position, use the smaller original index first.

Function
smallestReachableArray(nums: int[], k: int) → int[]

Examples
Example 1
nums = [3,1,2]
k = 1
return = [1,3,2]
Value 1 may move left to the first position. The original 3 must then be placed by index 1, because moving it two places right would exceed k.
Example 2
nums = [4,3,2,1]
k = 3
return = [1,2,3,4]
Every original element may move far enough right for the fully sorted order to be reachable.
Example 3
nums = [2,1,1]
k = 0
return = [2,1,1]
With k = 0, no element may move right. Any leftward move would force an earlier element right, so only the original order is reachable.

Constraints
0 <= nums.length <= 200000
0 <= k <= nums.length
-1000000000 <= nums[i] <= 1000000000

Reported by candidates. Source: FastPrep

Pattern and pitfall

Fill the output left to right. At output position i, any unused element with original index j <= i+k is eligible, since moving left is free. But an element at index j must land by position j+k. So at position i, check whether the element at index i-k is still unused. If it is, it's forced, so place it now. Otherwise take the smallest value among eligible elements, breaking ties by smaller original index. Use a min-heap keyed by (value, index), push indices as the window i+k advances, and mark placed indices in a boolean array. Pop lazily, skipping used ones. That's O(n log n). The common pitfall is forgetting the forced pick, which gives [1,2,3] style answers that break example 1. Another is the k = 0 case and an empty array. If the deadline rule slips under pressure, StealthCoder is your hedge during the live OA, but the logic is small enough to hold in your head.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Lexicographically Smallest Array with K-Limited Right Moves cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Lexicographically Smallest Array with K-Limited Right Moves FAQ

What's the trick in the Google k-limited right moves problem?+

Treat it as a greedy fill with a deadline. Each element can move left freely but must land by index j+k. At each output position, if the element at index i-k is unplaced, you must place it. Otherwise pop the smallest eligible value from a heap. That one forced check is what most people miss.

How hard is this one really?+

Medium to medium-hard. The heap part is routine. The difficulty is reading the movement rule correctly and realizing that deadlines force picks. Once you see eligibility as indices up to i+k and a forced element at i-k, the code is short. Expect most bugs to come from off-by-one errors.

Why can't I just sort the array?+

Sorting ignores the right-move limit. In example 1, nums = [3,1,2] with k = 1, sorted would be [1,2,3], but the 3 can't move two places right. It must be placed by index 1. Sorting only works when k is large enough, like example 2 with k = 3.

How do I handle duplicates and edge cases?+

Key the heap by value then original index, so equal values use the earlier index first, as the statement requires. Handle the empty array by returning an empty array. With k = 0 the forced check fires at every position, so the output equals the input. Test those three cases before submitting.

How do I prepare for this in 48 hours?+

Write the heap solution once from scratch on your own example, then trace example 1 by hand to confirm the forced pick. Practice lazy deletion with a used array. Also rehearse complexity: O(n log n) time and O(n) space. That's enough for this pattern. Don't spend time on unrelated permutation theory.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it