Compare Version Numbers
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
A version string is a list of non-negative integer **revisions** separated by dots, such as "1.02.10". A revision may carry leading zeros, which do not change its value ("02" equals "2").
To compare version1 and version2, compare their revisions from left to right as integers. When one version has fewer revisions, treat each missing revision as 0, so "1.0" equals "1.0.0".
Return -1 if version1 is smaller, 1 if it is larger, and 0 if the two are equal.
Examples
Constraints
- 1 <= version1.length, version2.length <= 500
- Both strings contain only digits and dots.
- Each string is a valid version: no leading, trailing, or consecutive dots.
- Every revision fits in a 32-bit integer.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.