Skip to content
RED SAGSkills
medium30 ptsdevopsparsingdates

Does a Cron Expression Match?

A scheduler checks every minute whether a job should run. The cron expression expr has five space-separated fields: minute (0-59), hour (0-23), day of month (1-31), month (1-12) and day of week (0-7, where both 0 and 7 mean Sunday).

Each field is a comma-separated list of items. An item is * (every value), a number n, a range a-b, or * or a-b followed by /s (every s-th value starting from the low end). isoTime is a UTC timestamp like 2024-03-11T09:30:00Z; seconds are ignored.

Minute, hour and month must all match. For the day: if both day-of-month and day-of-week fields are restricted (anything other than exactly *), the day matches when either one matches; otherwise both must match. Return true or false.

Example 1

Input: expr = "*/15 9-17 * * 1-5", isoTime = "2024-03-11T09:30:00Z"
Output: true
Explanation: Monday 09:30 matches every-15-minutes during office hours on weekdays.

Example 2

Input: expr = "0 12 * * 1-5", isoTime = "2024-03-09T12:00:00Z"
Output: false
Explanation: Saturday is outside 1-5.

Constraints

  • expr is valid
  • isoTime is between 1971 and 2099
Hint 1

Expand each field into a Set of allowed numbers.

Hint 2

Use new Date(Date.UTC(y, m - 1, d)).getUTCDay() for the weekday.

Good practice for: Site Reliability Engineer, DevOps Engineer, Cloud Engineer, Backend Developer, Automation Test Engineer, Data Engineer

Run checks your code on 3 sample tests. Submit runs all 10 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.