Solve Two OA Algorithm Problems
AmazonSoftware Engineer · Technical ScreenLast updated
These two problems evaluate proficiency with greedy algorithms, array manipulation, simulation of stateful processes, and feasibility analysis under capacity constraints.
The post describes two separate coding questions from an online assessment:
- Greedy deletion by minimum value
Given an integer array
nums, repeatedly perform the following operation until every element has been deleted:
- Among all undeleted elements, choose the one with the smallest value. If multiple elements have the same value, choose the leftmost one.
- Add its value to the answer.
- Delete that element and its immediate neighbors in the original array (
i - 1andi + 1) if those neighbors have not already been deleted.
Return the final accumulated answer.
- Minimum emergency refill days
You manage a warehouse with capacity
max_productsand initial inventory0. On dayi, the inventory changes bytask[i]at the end of the day. Before processing a day, you may perform an emergency refill and increase the inventory by any amount, as long as the inventory after the refill does not exceedmax_products. Using a refill on a day counts as one refill day regardless of how much inventory you add.
Constraints:
- After each day, inventory must never exceed
max_products. - On days where
task[i] == 0, the end-of-day inventory must be non-negative. - On other days, negative inventory is temporarily allowed.
Return the minimum number of refill days needed to satisfy all constraints, or -1 if no valid plan exists.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.