Skip to main content

Date Range Generator - Solution & Explanation

MediumPremiumFree on FleetCode4 min read
Practice this problem

Problem Statement

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-DD
  • 0 <= The difference in days between the start date and the end date <= 1500
  • 1 <= step <= 1000

Approach Overview

Problem 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.

Solution

Code

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Precompute Array of DatesO(n)O(n)When you need all dates stored at once or must return a full list
Generator-Based IterationO(n)O(1)Best for large ranges or streaming results lazily

Video Solution

Leetcode questions IRL 🙃 • Alberta Tech • 986,997 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Date Range Generator easy or hard?
Date Range Generator is typically considered a medium-level problem. The logic is simple iteration, but candidates must understand JavaScript generators, date manipulation with the Date API, and how to produce values lazily.
Date Range Generator Python/Java solution
In Python, a similar approach can be implemented using a generator with yield and the datetime.timedelta class. In Java, the range can be produced using LocalDate and incremented with plusDays(step) inside a loop or stream.
How to solve Date Range Generator in O(n)?
Initialize a Date object with the start date and loop until it exceeds the end date. On each iteration yield the current date and increment it using setDate(current.getDate() + step). This processes each date exactly once, resulting in O(n) time.
What is the best approach for Date Range Generator?
The generator-based iteration approach is the best solution. It iterates from the start date to the end date while yielding each date lazily. This keeps time complexity at O(n) while maintaining O(1) space since only the current date object is stored.
Is Date Range Generator asked at Google/Amazon/Meta?
Problems involving date manipulation and generator patterns appear in frontend and JavaScript-focused interviews. Variations of range generators and lazy iteration are common in companies evaluating JavaScript fundamentals and API design skills.
What data structure is used in Date Range Generator?
The optimal solution relies on a generator function combined with the built-in Date object. The generator controls iteration flow while the Date object handles date arithmetic and comparisons.
What is the time complexity of Date Range Generator?
The time complexity is O(n), where n is the number of generated dates between the start and end range. Each iteration performs constant work: checking the date boundary, yielding the value, and incrementing the date by the step.

Ready to solve this problem?

Practice Date Range Generator with our built-in code editor and test cases.

Practice on FleetCode