Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Parse a bounded Roman board revision

Hardware interview practice

Parse a bounded Roman board revision

MediumFirmware AlgorithmsSystemVerilog

Parse an explicitly sized electrically erasable programmable read-only memory (EEPROM) field as a canonical Roman numeral from I through MMMCMXCIX without allocation or out-of-range reads.

Try it in the question bankReason first. Then compare.

Keep this exact question selected while you check your answer and review the full solution.

Practice this question →
Starting point

Question code

typedef enum { ROMAN_OK, ROMAN_EINVAL, ROMAN_ERANGE } roman_rc_t;
roman_rc_t roman_to_u16(const uint8_t *buf,
                        size_t len,
                        uint16_t *value);
// len is 1..15; a successful value is 1..3999.
Reviewed example

Work through one case

Input
bytes="MCMXCIV", length=7
Expected output
success; revision=1994

The subtractive pairs CM, XC, and IV are legal, and re-encoding 1994 reproduces the exact input, proving canonical spelling.

What to cover

Requirements

  1. Accept only uppercase I, V, X, L, C, D, and M and the six standard subtractive pairs.
  2. Return invalid-input errors before range errors, except that a fully parsed value above 3999 returns the range error.
  3. Reject noncanonical spellings by re-encoding the parsed value and comparing every input byte.
  4. Do not call strlen or modify the caller's output until the complete parse succeeds.
Continue practicing

Related questions

Firmware AlgorithmsFind device-tree depth without recursion→Firmware AlgorithmsAdd decimal lists from a fixed pool→Firmware AlgorithmsEnumerate calibration sums with fixed memory→
asic.fyi · Learn silicon end to end.info@asic.fyi