Compare Dotted Revision Values with Missing Zeros

Quick Overview

Compare dotted version revisions numerically while ignoring leading zeros and treating missing revisions as zero.

Compare Dotted Revision Values with Missing Zeros

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Compare two valid version strings as sequences of integer revisions separated by dots. Ignore leading zeros within a revision. Compare revisions from left to right; if one version ends sooner, treat each missing revision as zero. Return -1, 0, or 1 according as `version1` is smaller, equal, or greater. ```cpp int compareVersion(string version1, string version2); ``` ### Examples ```text Input: version1 = "2.003.0", version2 = "2.3" Output: 0 ``` ```text Input: version1 = "4.12", version2 = "4.9.99" Output: 1 ``` ### Constraints - Each string has length between 1 and 500. - Only decimal digits and dots occur; both are valid version numbers. - Every revision value fits in a signed 32-bit integer.

Overview: Compare dotted version revisions numerically while ignoring leading zeros and treating missing revisions as zero.

Google logo
Google
Date not reported
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Compare two valid version strings as sequences of integer revisions separated by dots. Ignore leading zeros within a revision. Compare revisions from left to right; if one version ends sooner, treat each missing revision as zero. Return -1, 0, or 1 according as version1 is smaller, equal, or greater.

int compareVersion(string version1, string version2);

Examples

Input: version1 = "2.003.0", version2 = "2.3"
Output: 0
Input: version1 = "4.12", version2 = "4.9.99"
Output: 1

Constraints

  • Each string has length between 1 and 500.
  • Only decimal digits and dots occur; both are valid version numbers.
  • Every revision value fits in a signed 32-bit integer.