Validate and Parse Cron Expression

Difficulty: Hard · Topics: Validation, Parsing, Scheduling

Difficulty: Hard  |  Tags: Parsing, Validation, Scheduling

Description

Given a cron expression string with 5 fields (minute, hour, day-of-month, month, day-of-week), validate it and return the parsed representation.

Format: * → minute hour day month weekday

Implement:

parseCron(expression) → CronSchedule | Error

Examples

Example 1:

Input:  "30 9 * * 1-5"
Output: { minute: [30], hour: [9], dayOfMonth: [1-31], month: [1-12], dayOfWeek: [1,2,3,4,5] }
Explanation: 9:30 AM on weekdays

Example 2:

Input:  "*/15 * * * *"
Output: { minute: [0,15,30,45], hour: [0-23], dayOfMonth: [1-31], month: [1-12], dayOfWeek: [0-6] }
Explanation: Every 15 minutes

Example 3 — Invalid:

Input:  "60 25 * * *"
Output: Error — "minute must be 0-59; hour must be 0-23"

Field Ranges

FieldRangeSpecial Characters
Minute0-59*, ,, -, /
Hour0-23*, ,, -, /
Day of Month1-31*, ,, -, /
Month1-12*, ,, -, /
Day of Week0-6*, ,, -, /

Constraints

Practice this problem

Related problems