Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Roman Numeral Converter

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Roman Numeral Converter. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Appfolio applications may need to display ordinal-style labels or parse legacy Roman numeral values. Implement a clean, class-based converter that supports both Roman numeral to integer and integer to Roman numeral conversions with strict validation.

Formal Specification

Create RomanNumeralConverter with a public method that accepts a value and a direction:

  1. For direction = "to_integer", value must be a canonical uppercase Roman numeral and the method returns an integer.
  2. For direction = "to_roman", value must be an integer from 1 through 3999 and the method returns its canonical uppercase Roman numeral.
  3. Raise ValueError for malformed Roman numerals, unsupported directions, integers outside the allowed range, booleans, or values of the wrong type.
  4. Provide a wrapper function named convert_roman(value, direction) that uses the class.

Canonical notation allows I, V, X, L, C, D, and M, including only valid subtractive pairs such as IV, IX, XL, XC, CD, and CM.

Constraints

  • Roman numeral values are limited to 1 through 3999
  • Input Roman numerals contain at most 15 characters
  • Roman numeral input must use uppercase canonical notation
  • Valid integer input must be an int, excluding bool
  • Invalid input raises ValueError

Function Signature

def convert_roman(value, direction):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output