# The Version Parade

> 1.0 before 2.0. Don't let the dots confuse you.

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

Domain: Python · Difficulty: easy · Seniority: L5

## Problem

Given semantic version strings in 'major.minor.patch' format, return them sorted from oldest to newest. Compare each component as an integer, not lexicographically.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **custom sorting with parsed keys**. Semantic version strings must be compared numerically, not lexicographically. It probes whether a candidate can parse structured strings into comparable tuples.

---

### Break down the requirements

#### Step 1: Parse each version string into numeric components

Split on `.` and convert each part to an integer. `'1.10.0'` becomes `(1, 10, 0)`.

#### Step 2: Sort using the numeric tuples as keys

Python's tuple comparison is lexicographic on elements, which gives correct version ordering.

---

### The solution

**Parsed numeric tuple sort key**

```python
def sort_versions(versions: list) -> list:
    def version_key(v):
        parts = v.split(".")
        nums = []
        for p in parts:
            nums.append(int(p))
        return tuple(nums)
    result = sorted(versions, key=version_key)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n log n) for sorting where n is the number of version strings. Parsing each version is O(1) since the number of components is bounded.
> 
> **Space:** O(n) for the sorted output.

> **Interviewers Watch For**
>
> Understanding WHY lexicographic sort fails: `'1.10.0' < '1.2.0'` lexicographically because `'1' < '2'`, but numerically `10 > 2`. This is the entire motivation for the problem.

> **Common Pitfall**
>
> Using `sorted(versions)` without a key. This gives lexicographic order, which incorrectly ranks `'1.10.0'` before `'1.2.0'`.

---

## Common follow-up questions

- What if versions had optional pre-release tags like '1.0.0-beta'? _(Tests parsing and comparing pre-release suffixes with specific precedence rules.)_
- What if some versions had fewer components (e.g., '1.0' vs '1.0.0')? _(Tests padding missing components with zeros before comparison.)_
- How does semver define precedence for build metadata? _(Tests knowledge that build metadata (after +) is ignored for precedence.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_version_parade)
- [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.