Skip to content
Katabench
Try free

Daily Temperatures

Medium About 20 min Free, no account needed

For each day, how long until a warmer one?

The brief

Given an array temperatures, return an array answer where answer[i] is the number of days until a strictly warmer temperature, or 0 if none follows.

The performance bar

The obvious approach scans forward from every day, O(n2). The hidden test is 200,000 strictly cooling days: every scan runs to the end and finds nothing. You need an approach where each day is processed a constant number of times rather than rescanned, O(n).

Starter code

C#
using System;

public class Solution
{
    // TODO: return an array where entry i is the number of days until a strictly warmer temperature (0 if none).
    public int[] DailyTemperatures(int[] temperatures)
    {
        throw new NotImplementedException();
    }
}

How it is graded

Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.

C# coding challenges →

Related guides

What it practices

Monotonic stack
Keep a stack in sorted order to answer next-greater and span questions in one pass.

Related katas