DSA Tracker

Problem

Medium

Integer to Roman

A medium Strings interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Strings
Sheets
1
Core for
14 roles

The problem

Convert a non-negative integer into its Roman numeral representation.

Example 1

Input
num=3
Output
"III"

Example 2

Input
num=58
Output
"LVIII"

Example 3

Input
num=1994
Output
"MCMXCIV"

Constraints

  • 1 <= num <= 3999

How to think about it

Updated 2026-09-29

Roman numerals are a greedy subtractive positional system with 13 discrete value tokens. By treating the 6 subtractive pairs (like IV for 4 and CM for 900) as independent building blocks on equal footing with single symbols, the translation reduces to repeatedly peeling off the largest denomination that fits.

Approaches, worst first

  1. Digit position lookup tables

    time O(1) · space O(1)

    Extract thousands, hundreds, tens, and units places using division and modulus arithmetic. Map each decimal place to its corresponding Roman numeral segment using four static string tables, then concatenate the parts. It requires four hardcoded arrays covering thirty combinations.

  2. Greedy value-symbol matchingWrite this one

    time O(1) · space O(1)

    Store all 13 standard value-symbol pairs sorted descending from 1000 ('M') down to 1 ('I'). Iterate through the pairs: while num is greater than or equal to the current pair's value, append its symbol to the result and subtract its value from num.

Where people lose marks · 3
  • Treating 4, 9, 40, 90, 400, and 900 through custom branching logic instead of table entries.
  • Repeated string concatenation without a buffer; Roman numerals for num <= 3999 are at most 15 characters, so a small array or string builder handles it cleanly.
  • Off-by-one in the loop condition: use >= rather than > so exact multiples of denominations are consumed.

The theory behind it

Strings — the ground this problem stands on. All Strings problems

What Strings is

A string is an ordered necklace of text characters, like letters printed along a ribbon of paper. Each character sits at an exact numeric slot, holding a glyph such as a letter, punctuation mark, or digit. In many programming languages, ribbons cannot be edited after creation, meaning changing a single character requires pressing an entirely new ribbon from scratch.

When to reach for it

Reach for string techniques when inputs consist of words, DNA sequences, serialized data formats, or sentences. Clues include questions testing palindromes, anagram matches, substring patterns, parenthesis balancing, or character frequency counts. Whenever an algorithm asks to transform capitalization, parse structured tokens, or compute edits between two phrases, string representations are the core subject.

How the pattern works

Think of characters as small integer codes ranging across standard character sets. Frequency tables with fixed sizes often replace heavy hash maps when tallying occurrences. For search tasks, maintain rolling state using character indices or sliding borders. When building output text through repeated appends, accumulate pieces inside a mutable list or string builder rather than concatenating strings directly, avoiding quadratic copy overhead.

More Strings problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .