Watch 10 video solutions for Date Range Generator, a medium level problem. This walkthrough by Alberta Tech has 986,997 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a start date start, an end date end, and a positive integer step, return a generator object that yields dates in the range from start to end inclusive.
The value of step indicates the number of days between consecutive yielded values.
All yielded dates must be in the string format YYYY-MM-DD.
Example 1:
Input: start = "2023-04-01", end = "2023-04-04", step = 1 Output: ["2023-04-01","2023-04-02","2023-04-03","2023-04-04"] Explanation: const g = dateRangeGenerator(start, end, step); g.next().value // '2023-04-01' g.next().value // '2023-04-02' g.next().value // '2023-04-03' g.next().value // '2023-04-04'
Example 2:
Input: start = "2023-04-10", end = "2023-04-20", step = 3 Output: ["2023-04-10","2023-04-13","2023-04-16","2023-04-19"] Explanation: const g = dateRangeGenerator(start, end, step); g.next().value // '2023-04-10' g.next().value // '2023-04-13' g.next().value // '2023-04-16' g.next().value // '2023-04-19'
Example 3:
Input: start = "2023-04-10", end = "2023-04-10", step = 1 Output: ["2023-04-10"] Explanation: const g = dateRangeGenerator(start, end, step); g.next().value // '2023-04-10'
Constraints:
new Date(start) <= new Date(end)start and end dates are in the string format YYYY-MM-DD0 <= The difference in days between the start date and the end date <= 15001 <= step <= 1000Problem Overview: Given a start date, end date, and step size in days, generate every date in the range using a JavaScript generator. Each iteration should yield the next valid date until the end date is reached.
Approach 1: Precompute Array of Dates (O(n) time, O(n) space)
The straightforward approach is to iterate from the start date to the end date and push each computed date into an array. Use JavaScript's Date object to increment the current date by the given step using setDate(current.getDate() + step). Continue the loop while the current date is less than or equal to the end date. Finally return the array. This works well for small ranges but stores all results in memory, which becomes inefficient for large ranges where you only need values lazily.
Approach 2: Generator-Based Iteration (O(n) time, O(1) space)
The optimal solution uses a JavaScript generator function. Instead of building an array, yield each date as it is computed. Start with a Date instance created from the input start string. On every iteration, check whether the current date is still within the range. If it is, yield the formatted date and advance the date by the step value using setDate. This approach produces values lazily, meaning the next date is only computed when the caller requests it. Memory usage stays constant because only the current date object is stored.
The key insight is that the problem naturally fits the generator pattern. You are producing a sequential stream of values where the total size may be large. Instead of materializing the entire list, a generator emits one value at a time. Internally the algorithm is still a simple loop performing iteration over the range while applying date arithmetic using the JavaScript Date API.
Each step advances the date by a fixed number of days. Because the algorithm performs a constant amount of work per iteration (comparison, yield, and date update), the runtime grows linearly with the number of generated dates.
Recommended for interviews: The generator-based iteration approach. It demonstrates understanding of lazy evaluation, efficient memory usage, and practical use of JavaScript generators. Mentioning the array-based brute force approach first shows baseline reasoning, but the generator solution highlights stronger engineering judgment and language-specific knowledge.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Precompute Array of Dates | O(n) | O(n) | When you need all dates stored at once or must return a full list |
| Generator-Based Iteration | O(n) | O(1) | Best for large ranges or streaming results lazily |