AMAZON Coding Question – Solved

11 Live
The Amazon warehouse receives a multitude of packages every day, each assigned a unique identifier from 1 to n. The warehouse manager must sort the packages, not by their identifiers, but rather through a specific process defined by a permutation packageSequence that ensures optimal processing. Initially, the number of sorted packages, sortedCount, is zero. In each operation, the manager sifts through the packages from left to right. If the current package's identifier aligns with the next one to be sorted (i.e., packageSequence[i] = sortedCount + 1), the manager sorts it and increments sortedCount by one. If the current package is not next in the sorting sequence, the manager overlooks it. Determine the number of operations required by the manager to sort all the packages. One operation indicates a complete check from the first package to the last. Note: A permutation is a sequence consisting of integers from 1 to n, of length n, containing each integer exactly once. For example, [1, 3, 2] is a permutation, while [1, 2, 1] is not. Example 1 n = 5 packageSequence = [5, 3, 4, 1, 2] Initially sortedCount = 0. In the first operation, the manager does the following: At the end of the third operation, sortedCount is 5, and all packages are sorted. Thus, 3 operations are required to handle all the packages. Hence, the answer is 3. Example 2 n = 5 packageSequence = [3, 1, 4, 2, 5] In the first operation, the manager: 1. Overlooks packageSequence[0] = 3. 2. Sorts packageSequence[1] = 1 as packageSequence[1] = sortedCount + 1 = 1 (now sortedCount becomes 1). 3. Overlooks packageSequence[2] = 4. 4. Sorts packageSequence[3] = 2 as packageSequence[3] = sortedCount + 1 = 2 (now sortedCount becomes 2). 5. Overlooks packageSequence[4] = 5. At the end of the first operation, sortedCount is 2. In the second operation, the manager: 1. Sorts packageSequence[0] = 3 as packageSequence[0] = sortedCount + 1 = 3 (now sortedCount becomes 3). 2. Overlooks packageSequence[1] = 1. 3. Sorts packageSequence[2] = 4 as packageSequence[2] = sortedCount + 1 = 4 (now sortedCount becomes 4). 4. Overlooks packageSequence[3] = 2. 5. Sorts packageSequence[4] = 5 as packageSequence[4] = sortedCount + 1 = 5 (now sortedCount becomes 5). At the end of the second operation, sortedCount is 5, and all packages are sorted. Thus, 2 operations are required to handle all the packages. Hence, the answer is 2. Function Description Complete the function getNumberOperations in the editor below. getNumberOperations has the following parameters: int packageSequence[n]: an array of integers symbolizing the sequence of packages. Returns int: the number of operations the warehouse manager has to perform to sort all packages. Constraints Β· 1 ≀ n ≀ 10^6 Β· 1 ≀ packageSequence[i] ≀ n Β· packageSequence is a permutation of length n.

Asked in: AMAZON

Image of the Question

Question Image Question Image Question Image Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| Stacey is coordinating a beach clean-up event with her university's Women in ST… |
| Perform a series of operations on a given array of integers. Each operation con… |
| Data scientists at Amazon are working on a utility for detecting similar passwo… |
| To be efficient, Amazon must optimally distribute parcels among their delivery … |
| As an operations engineer at Amazon, you are responsible for organizing the dis… |
| Amazon Books is a retail store that sells the newly launched novel "The Story o… |