Compute days until plants stop dying
Company: Walmart Labs
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to reason about array-based state transitions and algorithmic complexity, testing competency in analyzing iterative dependencies among elements and handling large input sizes.
Constraints
- 1 <= len(plant) <= 100000
- 0 <= plant[i] <= 1000000000
Examples
Input: ([6, 5, 8, 4, 7, 10, 9],)
Expected Output: 2
Explanation: After day 1, plants 8 and 10 die. After day 2, plants 7 and 9 die. Then the row is stable.
Input: ([4],)
Expected Output: 0
Explanation: A single plant has no left neighbor, so it never dies.
Input: ([1, 2, 3, 4],)
Expected Output: 1
Explanation: Every plant except the first has a greater pesticide level than its left neighbor, so they all die on day 1.
Input: ([4, 3, 2, 1],)
Expected Output: 0
Explanation: No plant has a greater pesticide level than the plant immediately to its left, so the row is already stable.
Input: ([3, 3, 3, 3],)
Expected Output: 0
Explanation: Equal pesticide levels do not cause death because the rule is strictly greater.
Input: ([3, 6, 2, 7, 5],)
Expected Output: 2
Explanation: Day 1 removes 6 and 7, leaving [3, 2, 5]. Day 2 removes 5, leaving [3, 2].
Hints
- A day-by-day simulation can become O(n^2) in the worst case. Try to determine when each plant dies during a single left-to-right scan.
- A monotonic stack can help you find the nearest plant to the left that can keep a plant alive, while also tracking how many days removed plants survived.