Leetcode #2777: Date Range Generator
In this guide, we solve Leetcode #2777 Date Range Generator in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

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.
Quick Facts
- Difficulty: Medium
- Premium: Yes
- Tags: JavaScript
Intuition
Dates progress in fixed day increments, so we can generate them one by one.
A simple loop that adds step days each time is both clear and efficient.
Approach
Parse the start and end strings into date objects, then yield each date in ISO format while advancing by step days.
Steps:
- Parse
startandendas dates. - While current date is within the range, yield it.
- Increment by
stepdays each iteration.
Example
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'
Python Solution
from datetime import date, timedelta
def dateRangeGenerator(start: str, end: str, step: int):
cur = date.fromisoformat(start)
last = date.fromisoformat(end)
delta = timedelta(days=step)
while cur <= last:
yield cur.isoformat()
cur += delta
Complexity
The time complexity is , where is the number of yielded dates. The space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.