# The Date Sorter

> Jumbled calendar. Sort it first.

Canonical URL: <https://datadriven.io/problems/the_date_sorter>

Domain: Python · Difficulty: medium · Seniority: L3

## Problem

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

## 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**

```python
def sort_dates(dates: list) -> list:
    sorted_dates = sorted(dates)
    return sorted_dates
```

> **Time and Space Complexity**
>
> **Time:** O(n log n) for the sort, where n is the number of date strings.
> 
> **Space:** O(n) for the sorted copy.

> **Interviewers Watch For**
>
> Whether you overcomplicate this by parsing dates into `datetime` objects. The ISO format is designed to be lexicographically sortable. Recognizing this shortcut demonstrates practical knowledge.

> **Common Pitfall**
>
> Parsing dates unnecessarily adds complexity and runtime. However, if the format were not ISO (e.g., `MM/DD/YYYY`), you would need to parse before sorting.

---

## Common follow-up questions

- What if dates were in 'MM/DD/YYYY' format? _(Tests parsing with `datetime.strptime` and sorting by the parsed object.)_
- How would you sort dates from multiple time zones? _(Tests converting to UTC before comparison.)_
- What if you needed to sort in descending order? _(Tests the `reverse=True` parameter.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_date_sorter)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.