Min Operations to Sort All Packages
Updated
Problem statement
In an Amazon distribution center, a large number of parcels arrive daily, each assigned a distinct identifier ranging from 1 to n. The warehouse supervisor is tasked with organizing these parcels, but not by their identifiers. Instead, they need to follow a specific order provided by the sortingSequence, which is a permutation of the integers from 1 to n, ensuring the most efficient handling.
Initially, no parcels have been organized (arrangedCount is 0). The supervisor checks the parcels from left to right. If a parcel's identifier matches the next one that needs to be organized (i.e., sortingSequence[i] == arrangedCount + 1), the supervisor arranges it and increments arrangedCount by one. If a parcel's identifier does not match the expected one in the permutation sequence, it is skipped.
Your task is to determine how many complete checks (operations) the supervisor must perform to organize all the parcels. Each operation involves checking all parcels from the first to the last.
Note: A sortingSequence is a valid permutation, meaning it is a sequence consisting of integers from 1 to n, each appearing exactly once. For example, [1, 3, 2] is a valid permutation, while [1, 2, 1] is not.
Function
packageSorting(sortingSequence: int[]) → intExamples
Example 1
sortingSequence = [5, 3, 4, 1, 2]return = 3The supervisor repeatedly checks the parcels from left to right, arranging each parcel whose identifier equals arrangedCount + 1.
- Operation 1 arranges 1 and 2 (from positions of values 1 and 2).
- Operation 2 arranges 3 and 4.
- Operation 3 arranges 5, completing all parcels.
So, 3 operations are required to organize all the parcels.
Constraints
1 <= n, wherenis the length ofsortingSequencesortingSequenceis a permutation of the integers from1ton, with each integer appearing exactly once1 <= sortingSequence[i] <= n
More Amazon interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
Similar questions at other companies
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Book Reading EveningsGoogle · OAGoogle interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Wind-Blown Leaves Remaining in a GridAbridge · OAAbridge interview questions
- Shell Command Counts with History ReferencesTikTok · OATikTok interview questions
- Count Dropped RequestsJP Morgan Chase · OAJP Morgan Chase interview questions
- Recursively Expanded Shell Command CountsHudson River Trading · OAHudson River Trading interview questions
- Diagonal Bounce TraversalAutodesk · OAAutodesk interview questions
- First Value Below Both NeighborsCapital One · OACapital One interview questions
- REST API: Highest International StudentsRippling · OARippling interview questions