Roman to Integer
Reported by candidates from Navan's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Navan reportedly put Roman to Integer in front of candidates in October 2024, and it's easier than the invite email makes it feel. Strip the story and it's a lookup table plus one comparison per character. No tricks in the input: canonical uppercase numerals, values 1 through 3999, length up to 15. If you've seen it before, it's a five-minute solve. If you haven't, the whole thing hinges on one idea. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but you probably won't need it once you see the shape.
The problem
Given the canonical uppercase Roman-numeral representation of an integer from 1 through 3999, return its integer value. The subtractive pairs are IV, IX, XL, XC, CD, and CM. Function romanToInteger(roman: String) → int Examples Example 1 roman = "III" return = 3 The signed local contribution of each symbol reproduces canonical additive and subtractive notation. Example 2 roman = "IV" return = 4 The signed local contribution of each symbol reproduces canonical additive and subtractive notation. Example 3 roman = "IX" return = 9 The signed local contribution of each symbol reproduces canonical additive and subtractive notation. Constraints 1 <= roman.length <= 15. roman is the canonical uppercase representation of a value from 1 through 3999.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The problem reduces to a signed sum. Map each symbol to its value: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. Walk the string left to right. If the current symbol is smaller than the next one, subtract it. Otherwise add it. That single rule covers all six subtractive pairs (IV, IX, XL, XC, CD, CM) without hardcoding any of them. The common pitfall is the last character. Check the bounds so you don't read past the end, or iterate right to left and compare against the previous value. Another mistake is special-casing each pair with string replacement, which works but invites typos. Complexity is O(n) time and O(1) space. With length capped at 15, nothing here is about performance. It's about clean logic. If you freeze on the comparison direction during the live OA, StealthCoder can show the working solution while you stay in control of what you submit.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Roman to Integer 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as roman to integer. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Navan's OA.
Navan reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Roman to Integer FAQ
How hard is Roman to Integer really?+
Easy. It's a lookup table and a single pass. The Navan version caps the string at 15 characters and guarantees valid canonical input, so there's no validation work. Most people finish in under ten minutes once they remember the subtract-if-smaller-than-next rule.
What's the trick to Roman to Integer?+
Compare each symbol's value to the next one. If it's smaller, subtract it from the total. Otherwise add it. This handles IV, IX, XL, XC, CD and CM automatically, so you never need to list the subtractive pairs by hand.
Do I need to validate the input?+
No. The problem states the input is canonical uppercase Roman for values 1 through 3999. Skip validation, and don't waste time on lowercase handling or invalid sequences like IIII. Just map and sum.
Should I loop left to right or right to left?+
Either works. Left to right needs a bounds check on the next character. Right to left keeps the previous value and subtracts when the current one is smaller. Pick whichever you can write without an off-by-one error.
How do I prepare for this in 48 hours?+
Write it once from memory with the value map and the comparison rule. Test IV, IX, XLII, MCMXCIV and III. Then rehearse the inverse, Integer to Roman, since it's a common follow-up. That covers this whole family of questions.