← All Exercises

Count Cron Runs Inside a Window

intermediatejavascript~25min

Background

Schedule collisions are a common cause of duplicated work. Counting firings in a window makes it obvious when two jobs contend for the same minute.

Problem

For a simple cron minute field, count how many scheduled runs fall inside a time window and list the first runs, so overlap and load estimates are not guesses.

Input

JSON object: { cron: "*/N * * * *" | "M * * * *", from: ISO string, to: ISO string } (window is [from, to))

Output

JSON object: { count, firstRuns: string[] }

Sample tests

Sample 1

Input: {"cron":"*/15 * * * *","from":"2026-01-01T00:00:00Z","to":"2026-01-01T01:00:00Z"}
Expected: {"count":4,"firstRuns":["2026-01-01T00:00:00.000Z","2026-01-01T00:15:00.000Z","2026-01-01T00:30:00.000Z"]}

Sample 2

Input: {"cron":"5 * * * *","from":"2026-01-01T00:00:00Z","to":"2026-01-01T02:00:00Z"}
Expected: {"count":2,"firstRuns":["2026-01-01T00:05:00.000Z","2026-01-01T01:05:00.000Z"]}

Your solution

Learning objective

Estimate scheduled load from cron semantics rather than inspection.