Software Version Sequence Evaluator

🎯 Level: Medium Score: 50 ⏱️ Estimated Time: 30 mins
📜 Problem Description

At Zoblik International, managing software and API versions is crucial for maintaining compatibility and ensuring smooth updates. Their versioning scheme follows a standard format: a sequence of non-negative integer revisions separated by dots (e.g., 1.2.3, 10.0.50). Each revision can consist of one or more digits and may contain leading zeros (e.g., 01 is equivalent to 1).

Your task is to implement a tool that compares two given software versions and determines their relative order. The comparison should follow these rules:

  • Versions are compared from left to right, revision by revision.
  • Each revision is treated as an integer. Leading zeros are ignored when determining the integer value (e.g., 01 and 1 are both treated as the integer 1).
  • If one version runs out of revisions while the other still has revisions, the remaining revisions of the longer version are treated as if they are '0'. For instance, 1.2.3 is greater than 1.2 because 1.2 is implicitly 1.2.0.

Your program should output 1 if the first version is greater, -1 if the second version is greater, and 0 if both versions are equivalent.

Input Format

The first line contains an integer 'T', representing the number of test cases.

For each test case:

  • The first line contains a string version1, representing the first software version.
  • The second line contains a string version2, representing the second software version.

Output Format

For each test case, print an integer on a new line:

  • 1 if version1 > version2.
  • -1 if version1 < version2.
  • 0 if version1 = version2.

Constraints

  • 1 <= T <= 100
  • 1 <= version1.length, version2.length <= 104
  • Each revision consists of digits and contains at least one digit.
  • Revisions are non-negative.
📌 Constraints
1 <= T <= 100; 1 <= version1.size, version2.size <= 10^4
📝 Sample Input/Output
Example 1: Basic Comparison
Input:
1
2.3.0
2.4.0

Output:
-1

Explanation:
Comparing "2.3.0" and "2.4.0":
1. First revisions: 2 vs 2. They are equal.
2. Second revisions: 3 vs 4. 3 is less than 4.
Therefore, "2.3.0" < "2.4.0".
Example 2: Leading Zeros
Input:
1
1.001.2
1.1.2

Output:
0

Explanation:
Comparing "1.001.2" and "1.1.2":
1. First revisions: 1 vs 1. Equal.
2. Second revisions: 001 vs 1. Both are treated as integer 1. Equal.
3. Third revisions: 2 vs 2. Equal.
Therefore, "1.001.2" = "1.1.2".
Example 3: Unequal Lengths
Input:
1
1.2.1.1
1.2

Output:
1

Explanation:
Comparing "1.2.1.1" and "1.2":
1. First revisions: 1 vs 1. Equal.
2. Second revisions: 2 vs 2. Equal.
3. Third revisions: 1 vs (implicit) 0. 1 is greater than 0.
Therefore, "1.2.1.1" > "1.2".
💡 Hints
  • 💡 Consider splitting the version strings into individual revision components using the dot ('.') as a delimiter.
  • 💡 Remember that each revision component should be treated as an integer for comparison. Use an appropriate method to convert string parts to numbers, making sure leading zeros are correctly handled (e.g., '01' is 1).
  • 💡 When comparing versions of different lengths, assume that any missing revision at the end of a shorter version is implicitly zero. Your comparison loop should continue as long as either version has remaining revisions or until a difference is found.
📚 Topics

String & Tries

🏢 Asked By Companies

Amazon, Intuit

😕 No submissions found

Start coding and your submissions will appear here.