Sort name and Roman numeral strings by name, then by numeral value

Quick Overview

Sort strings that pair a name with a Roman numeral, ordering first by name without regard to case and then by the integer value of the numeral. It tests Roman numeral parsing, composite sort keys and stable handling of ties while each string keeps its original casing.

Sort name and Roman numeral strings by name, then by numeral value

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a list of strings `names`, where each string contains a name followed by a space and a Roman numeral, sort the list first alphabetically by name (case-insensitive), then by the decimal value of the Roman numeral for entries with the same name. Return the sorted list. Preserve the original casing of each string in the output. ### Function Signature ```python def sort_roman_names(names: list[str]) -> list[str]: ... ``` ### Rules - Each string is `"<name> <numeral>"` with exactly one space. The numeral uses the uppercase symbols `I`, `V`, `X`, `L`, `C`, `D` and `M`. - Names are compared after lowercasing them, so `"anne"`, `"Anne"` and `"ANNE"` are the same name. A name that is a prefix of another comes first (`"Al"` before `"Alan"`). - When two entries have the same lowercased name and the same numeral value, they keep their relative order from the input. - The returned strings are the original strings, unchanged. ### Constraints - `1 <= len(names) <= 1000` - Each string contains exactly one space separating the name and the Roman numeral. - Names consist of uppercase and lowercase English letters. - Roman numerals are valid and represent values from 1 to 3999. ### Examples ```text Input: names = ["Louis IX", "Louis VIII", "David II", "David I"] Output: ["David I", "David II", "Louis VIII", "Louis IX"] ``` Every "David" entry comes before every "Louis" entry. Within a name the numerals sort by value, so David I (1) precedes David II (2) and Louis VIII (8) precedes Louis IX (9). ```text Input: names = ["Henry IV", "Henry I", "Henry IX", "Anne III", "Anne I"] Output: ["Anne I", "Anne III", "Henry I", "Henry IV", "Henry IX"] ``` "Anne" comes before "Henry". Anne I (1) precedes Anne III (3), and Henry I (1), Henry IV (4) and Henry IX (9) follow in value order. ```text Input: names = ["edward XIV", "Edward IV", "anne MCMXC", "Anne XL", "ANNE XL"] Output: ["Anne XL", "ANNE XL", "anne MCMXC", "Edward IV", "edward XIV"] ``` All three "anne" spellings are one name, so they sort by value: XL (40) before MCMXC (1990), and the two XL entries tie and keep their input order. The "edward" entries follow as IV (4) then XIV (14), each with its original casing.

Overview: Sort strings that pair a name with a Roman numeral, ordering first by name without regard to case and then by the integer value of the numeral. It tests Roman numeral parsing, composite sort keys and stable handling of ties while each string keeps its original casing.

Salesforce logo
Salesforce
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a list of strings names, where each string contains a name followed by a space and a Roman numeral, sort the list first alphabetically by name (case-insensitive), then by the decimal value of the Roman numeral for entries with the same name.

Return the sorted list. Preserve the original casing of each string in the output.

Function Signature

def sort_roman_names(names: list[str]) -> list[str]:
    ...

Rules

  • Each string is "<name> <numeral>" with exactly one space. The numeral uses the uppercase symbols I , V , X , L , C , D and M .
  • Names are compared after lowercasing them, so "anne" , "Anne" and "ANNE" are the same name. A name that is a prefix of another comes first ( "Al" before "Alan" ).
  • When two entries have the same lowercased name and the same numeral value, they keep their relative order from the input.
  • The returned strings are the original strings, unchanged.

Constraints

  • 1 <= len(names) <= 1000
  • Each string contains exactly one space separating the name and the Roman numeral.
  • Names consist of uppercase and lowercase English letters.
  • Roman numerals are valid and represent values from 1 to 3999.

Examples

Input: names = ["Louis IX", "Louis VIII", "David II", "David I"]
Output: ["David I", "David II", "Louis VIII", "Louis IX"]

Every "David" entry comes before every "Louis" entry. Within a name the numerals sort by value, so David I (1) precedes David II (2) and Louis VIII (8) precedes Louis IX (9).

Input: names = ["Henry IV", "Henry I", "Henry IX", "Anne III", "Anne I"]
Output: ["Anne I", "Anne III", "Henry I", "Henry IV", "Henry IX"]

"Anne" comes before "Henry". Anne I (1) precedes Anne III (3), and Henry I (1), Henry IV (4) and Henry IX (9) follow in value order.

Input: names = ["edward XIV", "Edward IV", "anne MCMXC", "Anne XL", "ANNE XL"]
Output: ["Anne XL", "ANNE XL", "anne MCMXC", "Edward IV", "edward XIV"]

All three "anne" spellings are one name, so they sort by value: XL (40) before MCMXC (1990), and the two XL entries tie and keep their input order. The "edward" entries follow as IV (4) then XIV (14), each with its original casing.