Optimize Package Order by Sorting One Window
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in September 2026, and the detail that matters is in the statement: you sort exactly one window of length k, nothing else. That's it. The array is at most 200 long, so you don't need anything clever to pass. If you've got an OA coming up, expect a brute-force-friendly sorting problem with a lexicographic comparison twist. Try every window start, sort a copy of that slice, compare the full arrays, keep the smallest. StealthCoder sits as a quiet safety net on the live OA if your mind goes blank, but this one is very doable if you stay calm and read the constraints.
The problem
Given an integer array of package priorities and a window size k, choose exactly one contiguous window of length k and sort only that window in ascending order. Return the lexicographically smallest complete array obtainable by that operation. Function optimizePackageOrder(packages: int[], k: int) → int[] Examples Example 1 packages = [3,2,1,4] k = 3 return = [1,2,3,4] Sorting the first three values produces the smallest possible first element. Example 2 packages = [1,4,3,2] k = 2 return = [1,3,4,2] The window [4,3] is the first choice that improves the array lexicographically. Constraints 1 <= packages.length <= 200 1 <= k <= packages.length Package priorities fit in signed 32-bit integers.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that the constraint is tiny. With length up to 200, you can try every start index from 0 to n-k, copy the array, sort that slice, and compare the result lexicographically to your best so far. That's O(n * k log k), which is fine here. The common pitfall is the word exactly. You must pick one window even if sorting it makes nothing better, so initialize your best from the first candidate, not from the original array. Another trap is mutating the input in place and forgetting to restore it between tries. Also watch ties, since equal arrays don't matter. A smarter greedy exists, but it's riskier. If you freeze on the comparison logic or the copy handling during the live OA, StealthCoder is the hedge that hands you a clean working solution fast.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Optimize Package Order by Sorting One Window 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Optimize Package Order by Sorting One Window FAQ
How hard is Optimize Package Order by Sorting One Window really?+
Easy to medium. With n up to 200, brute force passes. The only real difficulty is reading carefully: you must sort exactly one window of length k, and the comparison is lexicographic on the whole array, not just the window.
What's the trick to this Amazon problem?+
Try every window start, sort a copy of that slice, and keep the lexicographically smallest full array. The small constraints make this safe. Don't overthink a greedy approach unless you've verified it against edge cases like already-sorted input.
Do I have to sort a window if the array is already sorted?+
Yes. The statement says choose exactly one window. Sorting a window of an already sorted array changes nothing, so you return the same array, but your code shouldn't skip the operation or crash on an empty candidate set.
How do I compare two arrays lexicographically in code?+
Walk both arrays from index 0 and stop at the first differing element. The smaller value at that index wins. Many languages compare lists or arrays this way natively, but in others you'll write a small helper loop.
How should I prepare in 48 hours for this kind of question?+
Practice slicing, copying, sorting a subrange, and lexicographic comparison in your chosen language. Write the brute-force version first, then test k=1, k=n, duplicates, and negative numbers. That covers nearly everything this problem can throw at you.