Compare Two Version Strings
Updated
Problem statement
A version string is a list of revisions separated by dots, such as 2.10.0. Each revision is a whole number written in decimal, possibly with leading zeros (so 010 means 10).
Compare version1 and version2 revision by revision from the left, as numbers. If one version has fewer revisions, its missing revisions count as 0. Return 1 if version1 is larger at the first revision where they differ, -1 if version2 is larger there, and 0 if no revision differs.
A revision can have up to 40 digits, which is more than a 64-bit integer can hold.
Function
compareVersions(version1: String, version2: String) → intExamples
Example 1
version1 = "3.14.0"version2 = "3.9"return = 1The first revisions are equal (3 and 3). The second revisions are 14 and 9, and 14 is larger, so the first version is newer: return 1.
Example 2
version1 = "2.005.0.0"version2 = "2.5"return = 0005 is the number 5, and the missing revisions of the second version count as 0. Both are 2, 5, 0, 0, so return 0.
Example 3
version1 = "7.2"version2 = "7.2.0.1"return = -1The first three revisions match (7, 2, 0). The fourth is missing in the first version, so it counts as 0, which is less than 1: return -1.
Constraints
1 <= version1.length, version2.length <= 500version1andversion2consist of digits and'.'.- Each version is one or more revisions joined by single dots (no dot at the start or the end, and no two dots in a row), and every revision has
1to40digits.
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Longest Palindromic SubstringSiemens · PHONE SCREEN
- Reverse Words in a StringAgoda · PHONE SCREENAgoda interview questions
- Longest Palindromic SubstringIBM · PHONE SCREENIBM interview questions
- Shortest Deletion for a Distinct RemainderGoogle · PHONE SCREENGoogle interview questions
- Shortest Way to Form a Target StringPinterest · PHONE SCREENPinterest interview questions
- Validate a Palindrome After Limited DeletionsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Snake-Case Iterator to Camel CaseNavan · PHONE SCREENNavan interview questions
- Reverse Word Order In PlaceMicrosoft · PHONE SCREENMicrosoft interview questions
- String CompressionBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe New Grad MTS Interview Questions: 7 Experiences
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)