FastPrepOptimize Package Order by Sorting One Window
Problems/Optimize Package Order by Sorting One Window

Optimize Package Order by Sorting One Window

Amazon logoAmazon● MediumFULLTIMEOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report provides a package list and a window size, permits sorting only within one window, and asks for the best resulting order. This practice contract uses lexicographic order and exactly one contiguous window.

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 = 3return = [1,2,3,4]

Sorting the first three values produces the smallest possible first element.

Example 2

packages = [1,4,3,2]k = 2return = [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.

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
class Solution {
    public int[] optimizePackageOrder(int[] packages, int k) {
        // Write your code here.
    }
}
Checking account…