Compare Version Numbers
Updated
Problem statement
Given two version strings version1 and version2, compare them.
A version consists of revisions separated by dots. Convert each revision to an integer, ignoring leading zeroes, and compare revisions from left to right. If one version has fewer revisions, treat each missing revision as 0.
- Return
-1whenversion1is smaller. - Return
1whenversion1is larger. - Return
0when the versions are equal.
Function
compareVersion(version1: String, version2: String) → intExamples
Example 1
version1 = "1.2"version2 = "1.10"return = -1The second revisions are 2 and 10, so the first version is smaller.
Example 2
version1 = "1.01"version2 = "1.001"return = 0After leading zeroes are ignored, both second revisions equal 1.
Example 3
version1 = "1.0"version2 = "1.0.0.0"return = 0The missing revisions in the first version are treated as 0, so the versions are equal.
Constraints
1 ≤ version1.length, version2.length ≤ 500- Both strings are valid version numbers containing only digits and dots.
- Every revision fits in a signed 32-bit integer.
More InMobi Phone Screen questions
- House Robber IPHONE SCREEN · Seen Aug 2026
- Word Break IIPHONE SCREEN · Seen Aug 2026
- Bus Station Seat AllotmentPHONE SCREEN · Seen Mar 2026
- Find Missing and Duplicate IntegerPHONE SCREEN · Seen Mar 2026
- Frequency Equals Card ValuePHONE SCREEN · Seen Mar 2026
- Frog Position After T SecondsPHONE SCREEN · Seen Mar 2026
- IPOPHONE SCREEN · Seen Mar 2026
- Evaluate DivisionPHONE SCREEN · Seen Dec 2025
- Count Array DerangementsPHONE SCREEN · Seen Feb 2022
- Maximum Frequency StackPHONE SCREEN · Seen Feb 2022
Similar questions at other companies
- Minimum Rotations for the Longest Common PrefixWells Fargo · OAWells Fargo interview questions
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Evaluate an Arithmetic Expression Written in WordsBrex · PHONE SCREEN
- Longest Palindromic SubstringSiemens · PHONE SCREEN
- Top K Movies by Play Count from CSV DataTubi · PHONE SCREEN
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Reverse Words in a StringAgoda · PHONE SCREENAgoda interview questions