Write Table-Driven Unit Tests for a Semantic Version Comparator
Last updated: November 29, 2026
Quick Overview
Implement a comparator for semantic version strings including prerelease tags, then write a table-driven test suite that covers ordering rules, symmetry, and invalid input, explaining why each case is there.
0
0
0 solved
Implement a comparator for semantic version strings including prerelease tags, then write a table-driven test suite that covers ordering rules, symmetry, and invalid input, explaining why each case is there.
Google cares heavily about testing culture, and a phone screen sometimes asks for tests alongside a small function. Version comparison is a good vehicle because the naive string compare is wrong in several ways. The interviewer evaluates whether the test cases are chosen deliberately to pin down each rule.
What the Interviewer Expects
- Parses components as integers so 1.10.0 sorts after 1.9.0
- Implements prerelease precedence correctly
- Uses a table of cases with subTest so one failure does not hide others
- Checks antisymmetry by asserting the reversed comparison
- Includes invalid inputs and expects explicit errors
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you turn the comparator into a property-based test of total ordering?
- Which cases would catch an implementation that compares strings lexically?
- How would you support build metadata after a plus sign?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
compare(a, b)returns -1, 0, or 1.- Major, minor, patch compare numerically.
- A prerelease is lower than its release; prerelease identifiers compare numerically if both are numeric, numeric sort...
High-Level Design
Parse with a strict regex into a tuple of three ints and an optional list of prerelease identifiers. Compare the tuple first, then the prerelease lists with the rules above. The test suite is a table:...