KatInterview

Detect 8-Hour Cooling Streaks in a Flaky Forecast Feed

Arrays & HashingModel answer
MediumModel answerArrays & Hashingsliding-windowretry-backoffapi-integration

Last updated

Build a service that checks a city's latitude, pulls a 72-hour forecast from an unreliable API with proper retries, and returns every 8-hour span of strictly falling, gapless hourly temperatures.

Task

Write a function get_decreasing_windows(city_id) that reports every 8-hour stretch in which the forecast temperature falls at every step.

What the function must do

  1. Look up the city. Use a PostgreSQL query to get the latitude and longitude for city_id. Continue only if the city is near the equator, meaning latitude from -5 to 5 degrees inclusive.
  2. Fetch the forecast. Request the next 72 hours of hourly data from an external weather service. It returns a list of records shaped like {'timestamp': 'YYYY-MM-DDTHH:MM:SSZ', 'temp': float}.
  3. Handle an unreliable service.
    • Retry server errors (5xx) using exponential backoff.
    • On a 429 response, wait for the duration given in the Retry-After header before trying again.
  4. Find the windows. A window is 8 consecutive hourly readings t[i], t[i+1], ..., t[i+7] where t[i] > t[i+1] > ... > t[i+7].
  5. Respect data continuity. If any hour is absent from the response inside a candidate window, that window is invalid.

Signature

def get_decreasing_windows(city_id: int) -> List[Tuple[str, str]]:
    # returns (start_timestamp, end_timestamp) for each valid window

Example

Temperatures, one per hour starting at 2023-10-01T00:00:00Z: [30, 29, 28, 27, 26, 25, 24, 23, 22, 24]

  • Indices 0-7: 30 ... 23, strictly decreasing, valid.
  • Indices 1-8: 29 ... 22, strictly decreasing, valid.
  • Indices 2-9: ends with 22, 24, not valid.

Result: [('2023-10-01T00:00:00Z', '2023-10-01T07:00:00Z'), ('2023-10-01T01:00:00Z', '2023-10-01T08:00:00Z')]

Edge cases to cover

  • Missing hours in the response.
  • A city outside the equatorial band.
  • Fewer than 8 data points in total.

What to cover in your answer

Describe how you would structure the code, the retry and error policy, the window-detection algorithm with its complexity, and how you would test it. Include code.

Hints

Hint 1

Separate the work into three pieces: the database lookup plus latitude check, the HTTP client with retries, and a pure function that takes records and returns windows. The last one is easy to unit test with no mocks.

Hint 2

You do not need to re-examine 8 values for each starting index. Keep a running count of how many consecutive valid steps end at the current element, and reset it whenever the time difference is not exactly one hour or the temperature fails to drop.

Hint 3

Treat each failure class differently: retry network errors and 5xx with backoff and jitter, obey Retry-After on 429 (with an upper cap), and fail immediately on other 4xx. When retries run out, raise an error rather than returning an empty list, so callers can tell failure from 'no windows'.