Finding the busiest time-frame in a parking lot The text file containing cars' arrival and departure time will upload stuff from the parking lot.

Text will be like this:

10:10, 11:10
10:30, 12:40 
10:25, 11:10
11:10, 12:20


I created a ParkedCar class:

public class ParkerCar
{
    public DateTime ArrivalTime { get; set; }
    public DateTime DepartureTime { get; set; }
}


The method that parses the text file, fills into List&lt;ParkedCar&gt;, and sorts them by the cars' arrival time:

public class ParkingLotRepository
{
    public List&lt;ParkedCar&gt; getParkedCars()
    {
        List&lt;ParkedCar&gt; parkedCars = new List&lt;ParkedCar&gt;();
        foreach (string line in File.ReadLines(path))
        {
            string[] times = line.Split(',');
            bool isValid = true;
            DateTime dtStart;
            if (!DateTime.TryParseExact(times[0].Trim(), "HH:mm",
                CultureInfo.InvariantCulture,
                DateTimeStyles.NoCurrentDateDefault, out dtStart))
                isValid = false;

            DateTime dtEnd;
            if (!DateTime.TryParseExact(times[1].Trim(), "HH:mm",
                CultureInfo.InvariantCulture,
                DateTimeStyles.NoCurrentDateDefault, out dtEnd))
                isValid = false;

            if (isValid)
            {
                parkedCars.Add(new ParkedCar
                {
                    ArrivalTime = dtStart,
                    DepartureTime = dtEnd
                });
            }
        }
        parkedCars = parkedCars
            .OrderBy(c =&gt; c.ArrivalTime)
            .ThenBy(c =&gt; c.DepartureTime)
            .ToList();

        return parkedCars;
    }
}


I wrote the method shown below to find the busiest time-frame and how many cars were in the parking lot. I'm not sure if this is an effective way to do that:

public static ParkTimeframe getPickTimeframe(List&lt;ParkedCar&gt; cars)
{
    List&lt;ParkTimeframe&gt; timeFrames = new List&lt;ParkTimeframe&gt;();
    DateTime dtStart = cars.First().arrivalTime;
    DateTime dtEnd = cars.First().leavingTime;
    int count = 0;

    foreach (ParkedCar car in cars)
    {
        if (car.arrivalTime &lt;= dtEnd &amp;&amp; car.arrivalTime &gt;= dtStart)
        {
            count++;
        }
        else
        {
            timeFrames.Add(new ParkTimeframe
            {
                startTime = dtStart,
                endTime = dtEnd,
                countCars = count
            });
            dtStart = car.arrivalTime;
            dtEnd = car.leavingTime;
            count = 0;
        }
    }

    int maxCountCar = timeFrames.Max(x =&gt; x.countCars);
    return timeFrames.First(x =&gt; x.countCars == maxCountCar);
}

public class ParkedCar
{
    public DateTime arrivalTime { get; set; }
    public DateTime leavingTime { get; set; }
}


Also, is there a better way to do that using LINQ?
Naming Conventions

MSDN, General Naming Conventions


DO NOT use Hungarian notation:


[dt]Start in getPickTimeframe() --> DateTime currentFrameStartDate
[dt]End in getPickTimeframe() --> DateTime currentFrameEndDate

DO favor readability over brevity:


ParkedCar --> ParkingLog
getPickTimeframe(List&lt;ParkedCar&gt; [cars]) --> GetPeakTimeFrame(List&lt;ParkingLog&gt; parkingLogs)
public int [countCars]; --> public int TotalNumberOfParkedCars;
int [count] = 0; --> int currentFrameNumberOfParkedCars;


MSDN, Naming Guidelines / Capitalization Conventions
DO use PascalCasing for all public member, type, and namespace names consisting of multiple words:


[G]et[Pick]Time[F]rame() --> GetPeakTimeFrame()
class ParkTime[F]rame
ParkTime[F]rame.[S]tartTime
ParkTime[F]rame.[E]ndTime
ParkTime[F]rame.[C]ountCars --> Also see above for alternative name
ParkedCar.[A]rrivalTime
ParkedCar.[L]eavingTime



Implementation

Your current implementation, besides efficiency, is not working correctly.

For parkings as the following:

// Parking opens at 07:00
DateTime parkingOpenDate = DateTime.Now.Date.AddHours(7);

// t --xxxxxxxx--------------------------------07:02 - 07:10
// t ---xxxxxxxx-------------------------------07:03 - 07:11
// t --------------xxxxxxxx--------------------07:14 - 07:22
// t --------xxxxxxxxx-------------------------07:08 - 07:17
// t 001222223321112221111100000000000000000000

new ParkingLog[]
{
    new ParkingLog() { ArrivalTime = parkingOpenDate.AddMinutes(2), LeavingTime = parkingOpenDate.AddMinutes(10) },
    new ParkingLog() { ArrivalTime = parkingOpenDate.AddMinutes(3), LeavingTime = parkingOpenDate.AddMinutes(11) },
    new ParkingLog() { ArrivalTime = parkingOpenDate.AddMinutes(14), LeavingTime = parkingOpenDate.AddMinutes(22) },
    new ParkingLog() { ArrivalTime = parkingOpenDate.AddMinutes(8), LeavingTime = parkingOpenDate.AddMinutes(17) }
}


It should output

07:08.00 - 07:10:00 with 3 cars


However, it outputs:

07:02.00 - 07:10:00 with 2 cars


Here are my changes to the code, calculating the correct peak time frame, only assuming the LeavingDate of each ParkingLog (ParkedCar in your source) is greater than the ArrivalDate:

public class _113182
{
    private SortedList&lt;DateTime, int&gt; carParkCapacityTracker = new SortedList&lt;DateTime, int&gt;();

    public class ParkingTimeFrame
    {
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public int TotalNumberOfParkedCars { get; set; }

        public override string ToString()
        {
            return string.Format("{0:HH:mm.ss} - {1:HH:mm:ss} | #Cars: {2}", StartTime, EndTime, TotalNumberOfParkedCars);
        }
    }

    public class ParkingLog
    {
        public DateTime ArrivalTime { get; set; }
        public DateTime LeavingTime { get; set; }

        public override string ToString()
        {
            return string.Format("{0:HH:mm:ss} - {1:HH:mm:ss}", ArrivalTime, LeavingTime);
        }
    }

    private void TrackCarParkingAction(DateTime actionDate, int actionEffect)
    {
        if (!carParkCapacityTracker.ContainsKey(actionDate))
        {
            carParkCapacityTracker.Add(actionDate, actionEffect);
        }
        else
        {
            carParkCapacityTracker[actionDate] += actionEffect;
        }
    }

    private void TrackCarParkingAction(ParkingLog parkingLog)
    {
        TrackCarParkingAction(parkingLog.ArrivalTime, +1);
        TrackCarParkingAction(parkingLog.LeavingTime, -1);
    }

    public ParkingTimeFrame GetPeakTimeFrame(List&lt;ParkingLog&gt; parkingLogs)
    {
        carParkCapacityTracker.Clear();

        parkingLogs.ForEach(TrackCarParkingAction);

        int currentNumberOfParkedCars = 0;
        int maximumNumberOfParkedCars = 0;
        int maximumCapacityFrameOffset = 0;

        for (int currentFrameOffset =  0; currentFrameOffset &lt; carParkCapacityTracker.Count; currentFrameOffset++)
        {
            currentNumberOfParkedCars += carParkCapacityTracker.Values[currentFrameOffset];
            if (maximumNumberOfParkedCars &lt;= currentNumberOfParkedCars)
            {
                maximumNumberOfParkedCars = currentNumberOfParkedCars;
                maximumCapacityFrameOffset = currentFrameOffset;
            }
        }

        return new ParkingTimeFrame()
        {
            StartTime = carParkCapacityTracker.Keys[maximumCapacityFrameOffset],
            EndTime = carParkCapacityTracker.Keys[maximumCapacityFrameOffset + 1],
            TotalNumberOfParkedCars = maximumNumberOfParkedCars
        };
    }
}


And here is the code to test it:

public static void Main()
{
    Console.WriteLine("Enter to start..");
    Console.ReadLine();

    // Parking opens at 07:00
    DateTime parkingOpenDate = DateTime.Now.Date.AddHours(7);

    List&lt;ParkingLog&gt; parkingLogs = new List&lt;ParkingLog&gt;
    (
        // t --xxxxxxxx--------------------------------07:02 - 07:10
        // t ---xxxxxxxx-------------------------------07:03 - 07:11
        // t --------------xxxxxxxx--------------------07:14 - 07:22
        // t --------xxxxxxxxx-------------------------07:08 - 07:17
        // t 001222223321112221111100000000000000000000

        new ParkingLog[]
        {
            new ParkingLog() { ArrivalTime = parkingOpenDate.AddMinutes(2), LeavingTime = parkingOpenDate.AddMinutes(10) },
            new ParkingLog() { ArrivalTime = parkingOpenDate.AddMinutes(3), LeavingTime = parkingOpenDate.AddMinutes(11) },
            new ParkingLog() { ArrivalTime = parkingOpenDate.AddMinutes(14), LeavingTime = parkingOpenDate.AddMinutes(22) },
            new ParkingLog() { ArrivalTime = parkingOpenDate.AddMinutes(8), LeavingTime = parkingOpenDate.AddMinutes(17) }
        }
    );

    // Should output 07:08 - 07:10 with 3 cars
    Console.WriteLine("{0}", new _113182().GetPeakTimeFrame(parkingLogs));
}


Edit:

The OP told me that he tested the code and the code failed.

I tested it with the same input (as documented in the question by the OP) and it produced the correct result:

public static void Main()
{
    // According to the data provided by the OP
    // 10:10, 11:10
    // 10:30, 12:40 
    // 10:25, 11:10
    // 11:10, 12:20

    //       10:00    10:10    10:25    10:30    11:10    12:20    12:40    
    // CAR 1            xxxxxxxxxxxxxxxxxxxxxxxxxxx
    // CAR 2                              xxxxxxxxxxxxxxxxxxxxxxxxxxxx
    // CAR 3                     xxxxxxxxxxxxxxxxxx
    // CAR 4                                       xxxxxxxxxx

    // The peak time frame should be: 10:30 - 11:10 with 3 cars:

    List&lt;ParkingLog&gt; parkingLogs = getParkedCars();
    Console.WriteLine("{0}", new _113182().GetPeakTimeFrame(parkingLogs));
}


Output:

10:30.00 - 11:10:00 | #Cars: 3


Edit 2:

The OP provided new test data and asserted that the code fails with this data.

Here is my test, and it works as expected, or there is something wrong and we are not on the same page:

// According to the new data provided by the OP
//09:00, 15:00
//10:10, 11:10
//10:30, 12:40
//10:35, 11:20
//10:25, 11:10
//11:10, 12:20

//       09:00    10:10    10:25    10:30    10:35    11:10    11:20    12:20    12:40    15:00
// CAR 1   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// CAR 2            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// CAR 3                              xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// CAR 4                                       xxxxxxxxxxxxxxxxxxx
// CAR 5                     xxxxxxxxxxxxxxxxxxxxxxxxxxxx
// CAR 6                                                xxxxxxxxxxxxxxxxxxx

// The peak time frame should be: 10:35 - 11:10 with 5 cars:

List&lt;ParkingLog&gt; parkingLogs = getParkedCars();
Console.WriteLine("{0}", new _113182().GetPeakTimeFrame(parkingLogs));


And the output is:

10:35.00 - 11:10:00 | #Cars: 5