Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.dailyTemperatures(temperatures). Return for each day the number of days until a strictly warmer temperature, or zero when none occurs.

Starter code

class Solution:
    def dailyTemperatures(self, temperatures):
        pass
Test cases

sample

{
  "args": [
    [
      73,
      74,
      75,
      71,
      69,
      72,
      76,
      73
    ]
  ]
}

Expected: [1,1,4,2,1,1,0,0]

Wizard outline
  1. Step 1: Initialize Solution.dailyTemperatures

    Replace the empty starter with the first real state owned by Solution.dailyTemperatures. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Assemble the primary transition

    Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.

  3. Step 3: Pass the Sample case

    Complete the readable core algorithm for one representative Interview case. Keep unresolved indices with decreasing temperatures and resolve them when a warmer value arrives.

  4. Step 4: Harden the Equal Not Warmer boundary

    Repair the reviewed boundary and pass the complete submission contract. Indices remain on the stack only while no warmer day has appeared. The first later day that pops an index is strictly warmer and earlier than any future day, so the recorded distance is exactly the required wait.

Footguns and prerequisites
  • Equal temperatures are not warmer and must remain unresolved.
  • arrays strings two pointers sliding window
Reviewed references
Practice prerequisites
  • Maintain a Monotonic Stack(opens in a new tab)

    Maintain a Monotonic Stack isolates stack indices increase from bottom to top and their values are strictly increasing after invalid candidates are removed. That focused state discipline is required when implementing daily temperatures as a complete Interview Problem.

Recommended approach and implementation

Scan temperatures with a stack of unresolved indices. While the current temperature is strictly greater than the stack top's temperature, pop and record the index distance.

Why it works: Indices remain on the stack only while no warmer day has appeared. The first later day that pops an index is strictly warmer and earlier than any future day, so the recorded distance is exactly the required wait.

class Solution:
    def dailyTemperatures(self, temperatures):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        waits = [0] * len(temperatures)
        unresolved = []
        for day, temperature in enumerate(temperatures):
            while unresolved and temperatures[unresolved[-1]] < temperature:
                earlier = unresolved.pop()
                waits[earlier] = day - earlier
            unresolved.append(day)
        return waits