Skip to content
Coding · Medium · Parsing · Target O(fields × parts) · Senior

Cron expression matcher

Decide whether a 5-field cron expression (*, ranges, lists, steps) matches a given minute, with correct day-of-month/day-of-week semantics.

Statement

Implement matches(expr, minute, hour, dom, month, dow) for a 5-field cron expression supporting *, single values, ranges a-b, lists a,b,c and steps */n and a-b/n.

Standard cron has one famous quirk: when both day-of-month and day-of-week are restricted (not *), the job runs if either matches. Implement that.

Examples

matches("*/15 2 * * *", 30, 2, 7, 11, 4)  → True
matches("0 9-17 * * 1-5", 0, 18, 7, 11, 4)  → False
matches("0 0 1 * 1", 0, 0, 1, 11, 4)         → True   # dom=1 matches, dow doesn't → OR
matches("0 0 1 * 1", 0, 0, 2, 11, 4)         → False

Constraints

Reject malformed expressions with ValueError (wrong field count, out-of-range values, step of 0). Sunday is 0. No names (MON, JAN) required.

Open to everyone until Pro goes on sale · sign in to save your attemptSolve it in the editor with visible tests
Open in the app