Find Optimal Level
Updated
Problem statement
Amazon is dedicated to leveraging advanced methods to streamline stock movement across its distribution centers. In this scenario, you are assigned a challenge involving a specific process with a sequence of stock quantities.
More specifically, you are provided with an array of positive numbers, stockArr, containing n elements. Additionally, you are given two positive numbers, incVal and decVal, where incVal is less than or equal to decVal. You may execute the stock adjustment operation on stockArr any number of times, including not performing it at all.
The stock adjustment operation is defined as picking two distinct positions p and q in the array (0 ≤ p, q < n), increasing the value at stockArr[p] by incVal, and decreasing the value at stockArr[q] by decVal.
Our goal is to compute the highest achievable value of the minimum stock quantity in stockArr after completing all desired operations.
Note: During the adjustment process, some elements may become negative. However, once all operations have been completed, every number in stockArr must be strictly greater than 0.
Function
findOptimalLevel(stockArr: int[], incVal: int, decVal: int) → int Complete the function findOptimalLevel in the editor.
findOptimalLevel has the following parameters:
int stockArr[n]: the stock quantitiesint incVal: the amount added by one operationint decVal: the amount subtracted by one operation
Returns int: the largest possible value of the minimum stock quantity after the operations
Examples
Example 1
stockArr = [11, 1, 2]incVal = 2decVal = 3return = 3Operation (p = 0, q = 2) gives [13, 1, -1]; (p = 1, q = 0) gives [10, 3, -1]; (p = 2, q = 0) gives [7, 3, 1]; (p = 2, q = 0) again gives [4, 3, 3]. All values are positive and the minimum is 3. A minimum of 4 is impossible: raising 1 and 2 to at least 4 needs 2 + 1 = 3 increases, but 11 can give only 2 decreases while staying at least 4 (11 - 3 - 3 = 5, and a third would leave 2), so return 3.
Example 2
stockArr = [1, 5, 9]incVal = 2decVal = 2return = 5Operation (p = 0, q = 2) gives [3, 5, 7]; doing it again gives [5, 5, 5], minimum 5. A minimum of 6 would need 3 + 1 = 4 increases (1 to 7 takes three, 5 to 7 takes one), but 9 can give only 1 decrease while staying at least 6 (9 - 2 = 7), so return 5.
Constraints
2 <= n <= 3 * 10^5, wherenis the length ofstockArr1 <= stockArr[i] <= 10^91 <= incVal <= decVal <= 10^9
More Amazon interview questions
- Get Smallest Base SegmentOA · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
- Minimum Grid InconvenienceOA · Seen Nov 2026
- 3SumPHONE SCREEN · Seen Nov 2026
- Circular Primes Below a LimitPHONE SCREEN · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
- Split Array Largest SumAuxoAI · OA
- Data ReorganizationGoldman Sachs · OAGoldman Sachs interview questions
- Capacity to Ship Packages Within DaysMicrosoft · OA · ONSITE INTERVIEWMicrosoft interview questions
- Rock JumpingIMC Trading · OAIMC Trading interview questions
- Minimize Maximum Group DifferenceDRW · OADRW interview questions
- Array NullificationQRT · OA
- Minimum-Cost Solar Rooftop AllocationDeloitte · OADeloitte interview questions
- Three-Part Partition with Minimum Largest RangeLSEG · OALSEG interview questions
- K-Means ClusteringAtlassian · OAAtlassian interview questions
Amazon interview experiences
- Amazon SWE OA with AI Coding: 1 Experience
- Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
- Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
- Amazon BIE Intern SQL and Interview Questions: 17 Experiences
- Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
- Amazon SDE II (L5) Interview Questions: 5 Experiences