The Date Sorter

A medium Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
medium
Seniority
L3
Asked in
2K

Problem

Given a list of 'YYYY-MM-DD' date strings, return them sorted chronologically ascending. Lexicographic comparison happens to be correct for this format.

Jumbled calendar. Sort it first.

Example

Input:

dates = ["2024-03-15","2023-12-01","2024-01-05"]

Output:

["2023-12-01","2024-01-05","2024-03-15"]

Worked solution and explanation

Why this problem exists in real interviews

This tests whether a candidate can sort structured string data by parsing it into comparable components. It probes awareness that ISO 8601 date strings (YYYY-MM-DD) are lexicographically sortable, making this simpler than it first appears.


Break down the requirements

Step 1: Recognize the ISO format advantage

YYYY-MM-DD strings sort correctly with standard string comparison because year, month, and day are zero-padded and in descending significance order.

Step 2: Sort the list

A simple sorted() call produces the correct chronological order.


The solution

Lexicographic sort of ISO date strings
def sort_dates(dates: list) -> list:
    sorted_dates = sorted(dates)
    return sorted_dates

Common follow-up questions