# The Version Ranker

> Software versions follow their own ordering rules.

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

Domain: Python · Difficulty: medium · Seniority: L3

## Problem

Given a list of semantic version strings like '1.2.10', '1.10.0', return the list sorted from oldest to newest. Compare each dot-separated component as an integer (not as a string). All inputs have the same number of components.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **semantic version sorting**, which requires parsing structured strings into numerically comparable components. It probes string manipulation and custom sort key construction.

---

### Break down the requirements

#### Step 1: Parse version strings into integer tuples

Split each version on `.` and convert components to integers.

#### Step 2: Sort using the integer tuples

Python tuple comparison handles the element-by-element comparison automatically.

---

### The solution

**Integer tuple sort key for version strings**

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

> **Time and Space Complexity**
>
> **Time:** O(n log n) where n is the number of versions.
> 
> **Space:** O(n) for the sorted output.

> **Interviewers Watch For**
>
> Explicitly converting to `int` during parsing. Leaving components as strings causes `'10' < '2'` which is the exact bug the problem tests.

> **Common Pitfall**
>
> Lexicographic string sorting ranks `'1.10.0'` before `'1.9.0'` because `'1' < '9'` at the character level. Numeric conversion is essential.

---

## Common follow-up questions

- How would you handle versions with different numbers of components? _(Tests right-padding with zeros: treat `'1.0'` as `'1.0.0'`.)_
- What if the version string could contain non-numeric suffixes? _(Tests splitting off the suffix and handling it separately.)_
- How would you compare versions in a SQL query? _(Tests `SPLIT_PART` with `CAST` to integer for each component.)_

## Related

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