using System.Collections.Generic;
public static class Program
public static void Main()
Run(new List<DateTime>(){
new DateTime(2015, 1, 1), new DateTime(2016, 2, 1)
}, new List<DateTime?>(){
null, new DateTime(2016, 2, 13)
Run(new List<DateTime>(){
new DateTime(2015, 1, 1), new DateTime(2016, 1, 1)
}, new List<DateTime?>(){
Run(new List<DateTime>(){
new DateTime(2015, 1, 1), new DateTime(2016, 1, 1)
}, new List<DateTime?>(){
new DateTime(2015, 12, 31), null
new DateTime(2015, 1, 1),
new DateTime(2016, 1, 11)
new DateTime(2016, 1, 31),
new DateTime(2015, 1, 1),
new DateTime(2016, 2, 1),
new DateTime(2016, 3, 1),
new DateTime(2016, 4, 1),
new DateTime(2016, 1, 10),
new DateTime(2016, 1, 31),
new DateTime(2016, 2, 28),
new DateTime(2016, 3, 31),
new DateTime(2016, 4, 30),
new DateTime(2016, 1, 15),
public static void Run(List<DateTime> effectiveStartDates, List<DateTime?> effectiveEndDates)
var result = Validate(effectiveStartDates, effectiveEndDates);
if(result == string.Empty) result = "No Error";
Console.WriteLine(result);
public static string Validate(List<DateTime> effectiveStartDates, List<DateTime?> effectiveEndDates)
var result = string.Empty;
for(var i=0; i < effectiveEndDates.Count; i++){
if(infIndex == -1 && !effectiveEndDates[i].HasValue){
else if(infIndex > -1 && !effectiveEndDates[i].HasValue){
return "cannot have two infinite ranges";
for(var i=0; i < effectiveEndDates.Count; i++){
if(i!= infIndex && IsOverlap(effectiveStartDates[i], effectiveEndDates[i].Value, effectiveStartDates[infIndex], DateTime.MaxValue)){
return "Overlap with infinite date range";
for(var i=1; i < effectiveEndDates.Count; i++){
if(!effectiveEndDates[i - 1].HasValue){
result = "cannont have a previous billing address without and end date.";
for(var i=1; i < effectiveStartDates.Count; i++){
if(effectiveEndDates[i - 1].HasValue && effectiveEndDates[i - 1].Value > effectiveStartDates[i]){
result = string.Format("cannot have 2 active billing address. start:{0}, end:{1}", effectiveStartDates[i], effectiveEndDates[i-1].Value);
for(var i=0; i < effectiveStartDates.Count; i++){
if(effectiveEndDates.Count > i && effectiveEndDates[i].HasValue)
for(var j=0; j < effectiveStartDates.Count && i !=j; j++){
if(effectiveEndDates.Count > j && effectiveEndDates[j].HasValue)
if(IsOverlap(effectiveStartDates[i], effectiveEndDates[i].Value,
effectiveStartDates[j], effectiveEndDates[j].Value)){
return string.Format("Following ranges overlap {0} - {1} && {2} - {3}",effectiveStartDates[i], effectiveEndDates[i].Value,effectiveStartDates[j], effectiveEndDates[j].Value);
public static bool IsOverlap(DateTime startDate, DateTime endDate, DateTime otherStartDate, DateTime otherEndDate)
return (startDate > otherStartDate && startDate < otherEndDate) ||
(endDate > otherStartDate && endDate < otherEndDate);