Reported August 2019
Motivestring

Parse a Valid Roman Numeral

Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Motive OA. Under 2s to a working solution.
Founder's read

This Motive OA, reported in August 2019, looks like the easy Roman-to-integer problem, but it reduces to something else: parse the string, then prove it's canonical. The catch is that "IIII" has to return -1, so a plain left-to-right sum fails. If you've got an invite and 48 hours, the pattern is string parsing plus a round-trip check. StealthCoder sits invisibly on your screen as a safety net if you blank during the live assessment, but the idea is simple enough that you should own it before you start.

The problem

Given a string roman, return its integer value if it is the canonical uppercase Roman-numeral representation of a value from 1 through 3999. Return -1 otherwise.

Function
parseValidRoman(roman: String) → int

Examples
Example 1
roman = "MCMXCIV"
return = 1994
Example 2
roman = "IIII"
return = -1

Constraints
1 <= roman.length <= 20.
roman contains uppercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Here's the trick. Don't write a pile of validation rules for repeats, subtraction pairs and ordering. Parse the string into an integer with the usual rule: if a symbol is smaller than the next one, subtract it, otherwise add it. Then convert that integer back to Roman using the canonical greedy table (1000 M, 900 CM, 500 D, 400 CD, 100 C, 90 XC, 50 L, 40 XL, 10 X, 9 IX, 5 V, 4 IV, 1 I). If the rebuilt string equals the input, return the value. Otherwise return -1. That catches IIII, VX, IC, and anything else non-canonical in one comparison. Pitfalls: invalid characters, so map lookups need a guard, and values outside 1-3999. The Motive input is at most 20 characters, so efficiency isn't a concern. If you freeze on the validation logic during the live OA, StealthCoder can hand you the round-trip approach, but you can memorize it in ten minutes.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Parse a Valid Roman Numeral cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Motive's OA.

Motive reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Parse a Valid Roman Numeral FAQ

What's the trick in the Motive Roman numeral problem?+

Parse to an integer, then convert that integer back to canonical Roman using the greedy value table. If the rebuilt string matches the input exactly, return the value. Otherwise return -1. This avoids hand-coding every rule about repeats and subtraction pairs.

How hard is this one really?+

Easy to medium. Roman-to-integer is a standard warm-up. The extra step is rejecting non-canonical strings like IIII. With the round-trip check, the whole solution is about 30 lines.

Which invalid inputs should I test?+

Test IIII, VV, IC, VX, IL, and XM. All are non-canonical and should return -1. Also test valid edge cases: I returns 1, MMMCMXCIX returns 3999, and MCMXCIV returns 1994. Add a check for any character outside the seven valid symbols.

Do I need to validate the range 1 to 3999 separately?+

Mostly the round-trip handles it. MMMM parses to 4000, and your converter, built for 1 to 3999, either can't reproduce it or you add an explicit range check first. Adding the explicit check is the safer choice and costs one line.

How do I prepare for this in 48 hours?+

Write the parse function and the greedy converter from memory twice. Then run 10 or so invalid strings through the pair. Learn the 13-entry value table cold. That table is the whole problem, and once it's in your head the rest is typing.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Motive.

OA at Motive?
Invisible during screen share
Get it