Implement and use a version comparator
Company: Nextdoor
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
You are given up to 100,000 version-like identifiers (e.g., "1.0", "01.2.0", "2.0.0-alpha", "1.10.3"). Implement compare(a, b) that orders identifiers by:
(
1) split by '.', compare numeric segments as integers;
(
2) missing segments are treated as 0;
(
3) pre-release tags (e.g., '-alpha', '-beta', '-rcN') sort before the corresponding release; numeric build metadata after a '+' is ignored for ordering. Use your comparator to return the list sorted in ascending order. Discuss time and space complexity and edge cases (leading zeros, different lengths, non-numeric segments). Provide code.
Quick Answer: This question evaluates string parsing, comparator design, sorting algorithm efficiency, and handling of semantic versioning semantics such as numeric versus non-numeric segments, missing segments, and pre-release ordering for large identifier lists.