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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Roman to Arabic Numerals
00:00
5 left

Roman to Arabic Numerals

MediumPython

Problem

Luxoft Singapore's engineering tooling receives configuration values written as Roman numerals. Implement a converter that returns the corresponding Arabic integer only when the input is a valid canonical Roman numeral. Return -1 for malformed, noncanonical, empty, or out-of-range input.

Formal Specification

Implement roman_to_arabic(roman), where roman is a non-empty uppercase string containing only the symbols I, V, X, L, C, D, and M. Return an integer from 1 through 3999, or -1 when the input is invalid.

Canonical notation follows these rules:

  1. Symbols are ordered from largest to smallest, except for IV, IX, XL, XC, CD, and CM.
  2. I, X, C, and M may appear at most three times consecutively.
  3. V, L, and D may not repeat.
  4. Subtractive pairs may use only the six combinations listed above.
  5. Noncanonical forms such as IIII, IL, VX, and IIV are invalid.

Constraints

  • 1 <= len(roman) <= 15
  • roman contains only uppercase ASCII characters or may contain invalid characters that must be rejected
  • Valid output values satisfy 1 <= value <= 3999
  • Only IV, IX, XL, XC, CD, and CM are legal subtractive pairs
  • Return -1 for empty, malformed, noncanonical, or out-of-range input

Function Signature

def roman_to_arabic(roman):
Interviewer

Your question is Roman to Arabic Numerals. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.