Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Roman Numeral Conversion Method
00:00
5 left

Roman Numeral Conversion Method

HardPython

Problem

Global Healthcare Exchange systems may receive compact Roman numeral identifiers from legacy healthcare documents. Implement a parser that converts a Roman numeral to its integer value only when the numeral follows canonical Roman numeral rules.

Return the decoded integer for a valid numeral. Return -1 for an invalid numeral.

Formal Specification

Implement roman_to_integer(s), where s is a non-empty uppercase string containing only the characters I, V, X, L, C, D, and M. Valid values range from 1 through 3999 and must use canonical notation:

  • I, X, and C may precede only their valid subtractive partners: IV, IX, XL, XC, CD, or CM.
  • V, L, and D may not repeat.
  • No symbol may repeat more than three times consecutively.
  • Symbols must appear in descending place-value order, except for valid subtractive pairs.

The function returns an integer for valid input and -1 otherwise. Aim for a single left-to-right parse and explain how canonical validation prevents malformed strings from being accepted.

Constraints

  • 1 <= len(s) <= 15
  • s contains only uppercase Roman numeral letters
  • Valid values are between 1 and 3999
  • Return -1 for any non-canonical representation

Function Signature

def roman_to_integer(s):
Interviewer

Your question is Roman Numeral Conversion Method. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.