Compare Version Numbers
Medium · Math & Number Theory · about 15 minutes
Asked at Apple, Microsoft, Amazon, MongoDB, ByteDance.
Compare two version strings like "1.01" and "1.001": split on '.' and compare revisions left to right as integers (ignoring leading zeros); a missing revision counts as 0. Return -1 if version1 < version2, 1 if it's greater, and 0 if they're equal.