Date Range Generator - Problem
Create a Date Range Generator

You're tasked with building a generator function that produces a sequence of dates within a specified range. Given a start date, an end date (inclusive), and a step value representing days, your generator should yield dates in chronological order.

Key Requirements:
  • Return a generator object (not an array)
  • Include both start and end dates if they align with the step pattern
  • All dates must be in YYYY-MM-DD format
  • Handle month/year transitions correctly

This problem tests your understanding of JavaScript generators, date manipulation, and iterator patterns.

Input & Output

example_1.py โ€” Basic Date Range
$ Input: start = "2024-01-01", end = "2024-01-10", step = 3
โ€บ Output: ["2024-01-01", "2024-01-04", "2024-01-07", "2024-01-10"]
๐Ÿ’ก Note: Starting from 2024-01-01, we add 3 days each time: Jan 1 โ†’ Jan 4 โ†’ Jan 7 โ†’ Jan 10. Since Jan 10 equals our end date, it's included.
example_2.py โ€” Cross-Month Range
$ Input: start = "2024-01-29", end = "2024-02-05", step = 2
โ€บ Output: ["2024-01-29", "2024-01-31", "2024-02-02", "2024-02-04"]
๐Ÿ’ก Note: The generator correctly handles month transitions: Jan 29 โ†’ Jan 31 โ†’ Feb 2 โ†’ Feb 4. Feb 6 would exceed the end date, so it's excluded.
example_3.py โ€” Single Day Range
$ Input: start = "2024-12-25", end = "2024-12-25", step = 1
โ€บ Output: ["2024-12-25"]
๐Ÿ’ก Note: When start equals end, the generator yields exactly one date (the start/end date) regardless of step size.

Constraints

  • Start date will always be โ‰ค end date
  • Dates are in valid YYYY-MM-DD format
  • step is a positive integer (1 โ‰ค step โ‰ค 103)
  • Date range will not exceed 104 days
  • Years will be between 1900 and 2100

Visualization

Tap to expand
๐Ÿ“… Date Range Generator TimelineStartEndJan 1โœ“ Yield+3 daysJan 4โœ“ Yield+3 daysJan 7โœ“ Yield+3 daysJan 10โœ“ Yield+3 daysJan 13โœ— Stopโšก Generator Benefitsโ€ข Memory Efficient: O(1) space - only current date storedโ€ข Lazy Evaluation: Dates computed only when requestedโ€ข Scalable: Works efficiently for any range size
Understanding the Visualization
1
Set Starting Position
Place marker on start date
2
Check Boundary
Verify current date hasn't exceeded end date
3
Yield Current Date
Return current date in required format
4
Jump Forward
Move marker forward by step days
5
Repeat
Continue until past end date
Key Takeaway
๐ŸŽฏ Key Insight: Generators provide the perfect balance of memory efficiency and ease of use for sequence generation problems.
Asked in
Google 23 Microsoft 18 Amazon 15 Meta 12
42.3K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen