HexSpeak: Decimal to Speakable Hexadecimal

Coding · Easy · Free problem

Given a string $S$ that encodes a decimal integer $N$, convert $N$ to its HexSpeak representation, or return "ERROR" if it is not a valid HexSpeak word.

To build HexSpeak: convert $N$ to uppercase hexadecimal, then replace the digit 0 with the letter "O" and the digit 1 with the letter "I". The result is a valid HexSpeak word only if it consists solely of the letters A, B, C, D, E, F, I, O. If any digit 2 through 9 appears, return "ERROR".

Constraints

  • $S$ represents a decimal integer between 1 and
    0^{12}$ inclusive

Examples

  • $S = $ "257" $\to$ hex "101" $\to$ "IOI"
  • $S = $ "3" $\to$ hex "3" $\to$ "ERROR"
  • $S = $ "14" $\to$ hex "E" $\to$ "E"

Hints

  1. Strip the problem to its core: convert to hexadecimal, then check whether the result only uses speakable letters.
  2. Watch the bound:
    0^{12}$ does not fit in a 32-bit integer, so parse with a 64-bit or big-integer type.
  3. After mapping 0->O and 1->I, the valid character set is exactly {A,B,C,D,E,F,I,O}; anything else means ERROR.

Worked Solution

How to Think About It: This is base conversion plus a validation pass. The only wrinkle is the input size:

0^{12}$ exceeds a 32-bit integer, so in languages with fixed-width ints you must use 64-bit (or arbitrary precision) parsing. After converting to uppercase hex, swap 0->O and 1->I, then check that every character lands in the allowed letter set.

Algorithm: Parse $S$ to an integer (64-bit safe), convert to uppercase hex, replace '0' with 'O' and '1' with 'I', then verify the string matches only [A-F, I, O]; otherwise return ERROR.

Code: ```python import re

def solution(S): n = int(S) h = format(n, 'X') # uppercase hex, no 0x prefix h = h.replace('0', 'O').replace('1', 'I') return h if re.fullmatch(r'[ABCDEFIO]+', h) else "ERROR" ```

Complexity: $O(L)$ where $L$ is the number of hex digits (at most ~10 for

0^{12}$).

Answer: Convert to uppercase hex, map 0->O and 1->I, and return ERROR if any of 2-9 (i.e. any non-allowed letter) remains.

Intuition

The puzzle is a thin wrapper around radix conversion, and the interviewer is really checking whether you notice the overflow boundary -- that is why the input arrives as a string rather than an int. In production, off-by-a-bit integer overflow on large IDs or prices is a real and costly class of bug, so screens like this reward candidates who read the constraint line and pick the right numeric type instead of trusting a default parse.

Open the full interactive solver →