Fork me on GitHub

Project Notes

#489 firstFrost

Using python to find the first frost: cassidoo’s interview question of the week (2026-09-28).

Notes

The interview question of the week (2026-09-28):

Given an array of daily temperatures and a number drop, return an array where each element is how many days you’d wait until it’s at least drop degrees colder than that day. If that never happens, put 0.

Example:

> firstFrost([70, 68, 72, 60, 65, 55], 5)
> [3, 2, 1, 2, 1, 0]

> firstFrost([50, 49, 48], 5)
> [0, 0, 0]

> firstFrost([40, 30, 45, 20], 10)
> [1, 2, 1, 0]

Thinking about the Problem

For a given day, things are quite simple - scan ahead until the difference exceeds the drop.

The challenge is perhaps finding a smart way to do this without having to enumerate the array for each day separately. Can’t immediately think how, so it is perhaps best to just start with a naïve implementation and see if that prompts any ideas.

Initial Solution

Starting with a naïve solution:

  • scan the daily temperatures
  • for each entry, scan ahead counting the days until the temperature drop exceeds the limit
  • record the result and move on to the next day
def firstFrost(daily_temps, drop):
    result = []
    for i, temp in enumerate(daily_temps):
        days_to_frost = 0
        for j in range(i + 1, len(daily_temps)):
            if daily_temps[j] <= temp - drop:
                days_to_frost = j - i
                break
        result.append(days_to_frost)
    return result

And that seems to work ok:

$ ./challenge.py "[70, 68, 72, 60, 65, 55]" 5
# Given:
# * Daily temperatures: [70, 68, 72, 60, 65, 55]
# * Drop: 5
# Days to wait for frost:
[3, 2, 1, 2, 1, 0]
$ ./challenge.py "[50, 49, 48]" 5 2> /dev/null
[0, 0, 0]
$ ./challenge.py "[40, 30, 45, 20]" 10 2> /dev/null
[1, 2, 1, 0]

Tests

I’ve setup some validation in test_challenge.py:

$ ./test_challenge.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

Final Code

Final code is in challenge.py:

#! /usr/bin/env python
from sys import argv
from sys import stderr
import json


def firstFrost(daily_temps, drop):
    result = []
    for i, temp in enumerate(daily_temps):
        days_to_frost = 0
        for j in range(i + 1, len(daily_temps)):
            if daily_temps[j] <= temp - drop:
                days_to_frost = j - i
                break
        result.append(days_to_frost)
    return result


if __name__ == '__main__':
    if len(argv) == 3:
        daily_temps = json.loads(argv[1])
        drop = int(argv[2])
        print("# Given:", file=stderr)
        print("# * Daily temperatures:", daily_temps, file=stderr)
        print("# * Drop:", drop, file=stderr)
        print("# Days to wait for frost:", file=stderr)
        print(firstFrost(daily_temps, drop))
    else:
        print("Usage: challenge.py '<array of daily temperatures>' '<drop>'", file=stderr)

Credits and References

About LCK#489
pythoncassidoo

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

Project Source on GitHub Return to the LittleCodingKata Catalog
About LittleCodingKata

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

Follow the Blog follow projects and notes as they are published in your favourite feed reader