PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

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.

  • medium
  • Walmart Labs
  • Coding & Algorithms
  • Software Engineer

Compute days until plants stop dying

Company: Walmart Labs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a row of plants, each with an integer pesticide level. ## Rules - On each day, any plant with a **strictly greater** pesticide level than the plant **immediately to its left** dies. - All plants that die on a given day are removed **simultaneously**. - The leftmost plant never dies (it has no left neighbor). - The process repeats until a day occurs when **no plants die**. ## Task Given an array `plant[]` of pesticide levels from left to right, return the number of days until plants stop dying. ## Input/Output - **Input:** `plant` (array of integers) - **Output:** integer number of days until stable ## Example - Input: `plant = [6, 5, 8, 4, 7, 10, 9]` - Output: `2` ## Assumptions / Constraints (if not otherwise specified) - `1 <= n <= 1e5` - `0 <= plant[i] <= 1e9` - Aim for better than O(n²) time for large `n`.

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.

You are given a row of plants, where each plant has an integer pesticide level. On each day, any plant with a strictly greater pesticide level than the plant immediately to its left dies. All deaths on the same day happen simultaneously, and the surviving plants close ranks before the next day begins. The leftmost plant never dies because it has no left neighbor. Return the number of days until the plants reach a stable state where no plant dies anymore. Example: If plant = [6, 5, 8, 4, 7, 10, 9], then the answer is 2. - Day 1: plants with levels 8 and 10 die, leaving [6, 5, 4, 7, 9] - Day 2: plants with levels 7 and 9 die, leaving [6, 5, 4] - Day 3: no plants die, so the process stops after 2 days

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

  1. 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.
  2. 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.
Last updated: May 7, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement lexicographically smallest Two Sum - Walmart Labs (medium)
  • Check whether brackets are balanced - Walmart Labs (medium)
  • Count ways to make change (DP) - Walmart Labs (medium)
  • Find shared courses between student pairs - Walmart Labs (medium)
  • Merge two sorted arrays in-place - Walmart Labs (easy)