Interview question
Validate a date range request
Implements a small validation helper for date ranges with clear error messages.
TL;DR
Implements a small validation helper for date ranges with clear error messages.
Validation, boundary rules, error collection, date math, and API-friendly feedback.
Practice the problem like a real interview: restate, reason, implement, and test.
Implement ValidateDateRange(DateOnly? start, DateOnly? end, int maxDays) returning a list of error messages. Both dates are required. Start must be on or before end. The inclusive range cannot exceed maxDays.
Input: start 2026-01-01, end 2026-01-31, maxDays 31
Output: no errors
I would collect errors in a list so the API can return all field issues at once. First validate required values and maxDays. Only if both dates exist do I compare ordering and length. The inclusive day count is end.DayNumber - start.DayNumber + 1, which avoids time-of-day problems because DateOnly is already date-based.
public static IReadOnlyList<string> ValidateDateRange(
DateOnly? start,
DateOnly? end,
int maxDays)
{
var errors = new List<string>();
if (start is null)
{
errors.Add("Start date is required.");
}
if (end is null)
{
errors.Add("End date is required.");
}
if (maxDays <= 0)
{
errors.Add("Maximum range must be positive.");
}
if (start is not null && end is not null)
{
if (start > end)
{
errors.Add("Start date must be on or before end date.");
}
else if (maxDays > 0)
{
var days = end.Value.DayNumber - start.Value.DayNumber + 1;
if (days > maxDays)
{
errors.Add($"Date range cannot exceed {maxDays} days.");
}
}
}
return errors;
}
The method is O(1) time and O(1) space, excluding the small error list.
Next in Coding Practice: Fixed Window Limiter