Katkat

Implement version string comparator with extensions

NextdoorSoftware Engineer · Technical Screen

Last updated

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.

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).