Integer to Roman
Greedy Lookup / Paired Hash MapSolved Nov 7, 2025
Asked at 12 companies
7 recent
- IBM
- tcs
- Amazon
- Bloomberg
- Microsoft
- Meta
- X
- Verkada
- Adobe
+2 moreShow fewer
- Docusign
- UiPath
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven an integer N (guaranteed to be between 1 and 3999), convert it to its corresponding Roman numeral string.ReadHide
Problem Description
Given an integer N (guaranteed to be between 1 and 3999), convert it to its corresponding Roman numeral string.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Use a lookup table of Roman values and symbols, sorted from largest to smallest. Greedily subtract the largest possible value until the integer reaches zero.
Approaches
1. Greedy Lookup (Optimal) - O(1) ⭐
Idea: Since the Roman system has a fixed number of unique symbols and subtraction pairs (13 total), the process is a greedy algorithm. We repeatedly find the largest Roman value less than or equal to the remaining number and subtract it.
Time: O(1) (The number of operations is fixed, proportional to 13 lookups, not N).
Space: O(1) (Fixed-size lookup table).
Solution code
import sys
class Solution:
# Use paired lists or a tuple of tuples for the lookup table, ordered largest to smallest
MAPPING = [
(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")
]
# O(1) Time, O(1) Space - Optimal (Greedy Lookup)
def intToRoman_optimal(self, num: int) -> str:
result = []
for value, symbol in self.MAPPING:
if num == 0:
break
# Calculate how many times the symbol fits
count = num // value
if count > 0:
result.append(symbol * count)
num %= value
return "".join(result)
def solve(self):
try:
num = int(sys.stdin.readline().strip())
except:
return
result = self.intToRoman_optimal(num)
print(result)
if __name__ == "__main__":
Solution().solve()Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language