Expand a cron field

A free medium coding practice problem from sysadmin. Solve it in JavaScript, TypeScript or Python and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.

Difficulty
Medium
Type
Function
Topic
Sysadmin
Languages
JavaScript, TypeScript or Python

Problem

Expand one field of a crontab line into the sorted list of distinct values it matches. The field's allowed values are lo … hi (e.g. minutes 0–59, days 1–31). field is one or more comma-separated items; each item is one of:
- * — every value lo … hi
- a — the single value a
- a-b — every value from a to b inclusive
- */s — lo, lo+s, lo+2s, … up to hi
- a-b/s — a, a+s, … up to b
- a/s — a, a+s, … up to hi

Inputs are always valid: 0 ≤ lo ≤ a ≤ b ≤ hi ≤ 59, s ≥ 1, no spaces.

Function signature

// JavaScript
function expandCronField(field, lo, hi)

// TypeScript
function expandCronField(field: any, lo: any, hi: any): any

// Python
def expand_cron_field(field, lo, hi):

Examples

  1. expandCronField("*/15", 0, 59) → [0,15,30,45]
  2. expandCronField("1-5,10,20-30/5", 0, 59) → [1,2,3,4,5,10,20,25,30]
  3. expandCronField("5/20", 1, 31) → [5,25]

How checking works

Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and random inputs. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.

More practice problems

Next steps