Implement version string comparator with extensions
Company: Nextdoor
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Implement a function compareVersions(a, b) that compares two software version strings. Each version consists of dot-separated numeric components (e.g., 1.02.
3). Treat missing trailing components as 0 and compare numerically, ignoring leading zeros. Extend the comparator to support optional pre-release identifiers (e.g., -alpha, -rc.
1) that sort before the corresponding release; compare pre-release identifiers by dot-separated tokens where numeric tokens are compared numerically and non-numeric tokens lexicographically; ignore build metadata (everything after '+'). Return -1 if a < b, 0 if equal, and 1 if a > b. Analyze time and space complexity and describe test cases covering tricky inputs (very long components, many segments, leading zeros, empty segments, pre-release ordering).
Quick Answer: This question evaluates string parsing and comparison algorithms, handling of numeric versus lexical ordering and versioning semantics (including pre-release identifiers and ignored build metadata), within the Coding & Algorithms domain.
Part 1: Compare Dot-Separated Numeric Version Strings
Write a function `solution(a, b)` that compares two software version strings made only of dot-separated numeric components. Compare each component numerically, not lexicographically, so `'10' > '2'`. Ignore leading zeros inside a component. If one version has fewer components, treat the missing trailing components as `0`. Also treat empty components caused by consecutive dots or leading/trailing dots as `0`, so `'1..2'` is equivalent to `'1.0.2'` and `''` is equivalent to `'0'`. Return `-1` if `a < b`, `0` if they are equal, and `1` if `a > b`. Your solution should handle very long numeric components without relying on fixed-width integer conversion.
Constraints
- 0 <= len(a), len(b) <= 200000
- Each string contains only digits `0-9` and `.`
- The empty string is allowed and represents version `0`
- Empty components are allowed and should be treated as `0`
- A numeric component may be much longer than standard integer ranges
Examples
Input: ('1.02.3', '1.2.3.0')
Expected Output:
Explanation: Leading zeros are ignored, and the missing trailing component in the first version is treated as 0.
Input: ('1.0.5', '1.0.12')
Expected Output:
Explanation: Compare numerically: 5 < 12.