Skip to main content

16. Daily Temperatures

mediumAsked at Rappi

For each day, find how many days until a warmer temperature — Rappi frames this as how many dispatch ticks until a courier sees a higher-priority order than the current one.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given an integer array of daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after day i to get a warmer temperature. If no future day is warmer, set answer[i] = 0.

Constraints

  • 1 <= temperatures.length <= 10^5
  • 30 <= temperatures[i] <= 100

Examples

Example 1

Input
temps = [73,74,75,71,69,72,76,73]
Output
[1,1,4,2,1,1,0,0]

Example 2

Input
temps = [30,40,50,60]
Output
[1,1,1,0]

Approaches

1. Brute nested scan

For each day, scan forward until you hit a higher temperature.

Time
O(n^2)
Space
O(n)
const ans = new Array(temps.length).fill(0);
for (let i = 0; i < temps.length; i++)
  for (let j = i+1; j < temps.length; j++)
    if (temps[j] > temps[i]) { ans[i] = j-i; break; }
return ans;

Tradeoff:

2. Monotonic decreasing stack

Push indices onto a stack while temps decrease; pop and assign when a warmer value arrives — each index is pushed and popped once.

Time
O(n)
Space
O(n)
function dailyTemperatures(t) {
  const ans = new Array(t.length).fill(0);
  const st = [];
  for (let i = 0; i < t.length; i++) {
    while (st.length && t[i] > t[st.at(-1)]) {
      const j = st.pop();
      ans[j] = i - j;
    }
    st.push(i);
  }
  return ans;
}

Tradeoff:

Rappi-specific tips

Rappi treats the monotonic-stack pattern as table stakes — they'll ask 'what invariant does the stack maintain' to check you actually understand the amortized O(n).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Daily Temperatures and other Rappi interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →