Base and Radix
What is this?
A "base" (or radix) is simply how many distinct digits a number system uses before it rolls over to the next place. We count in base 10 with digits 0-9, computers love base 2 (just 0 and 1) and base 16, and even spreadsheet column letters are a quirky base 26. These problems convert a number from one base to another by either peeling off digits one at a time or folding them back together.
๐ก Fun fact: Spreadsheet column headers (A, B, ..., Z, AA, AB) are a base-26 system with no zero digit, which is why the math needs a sneaky "subtract one" step that trips up almost everyone the first time.
๐ The 3 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: every positional number is a sum of digit times radix-power, so you change bases by repeatedly dividing to peel off the least-significant digit, or repeatedly multiplying to fold digits back into a value.
The pattern
Base conversion is two mirror-image loops. To go from an integer to a string in base b, divide by b over and over: each remainder is the next digit (low to high), and the quotient feeds the next step until it reaches zero. To go from a string back to an integer, walk the characters left to right, multiplying the running total by b and adding the current digit โ Horner's method.
Two twists separate the easy cases from the interview-worthy ones. First, two's complement: negative integers have no minus sign in their bit pattern, so a hex conversion must treat the value as unsigned bits, masking four bits at a time and shifting right logically, not arithmetically. Second, bijective numbering: spreadsheet columns run A, B, ..., Z, AA, with no zero digit, so the usual divide-by-radix loop needs an off-by-one correction (subtract one before each division) to map between the integer and its title.
The problems
Convert a Number to Hexadecimal takes the raw 32-bit pattern and emits base-16. Mask the low four bits, map them to a hex character, shift right by four, and repeat until the value is zero; using unsigned shifts makes negatives fall out of the same loop without special-casing.
Excel Sheet Column Number is the multiply-and-accumulate direction: read "ZY" left to right, each letter contributing its 1-to-26 value as you scale the total by 26. This is plain base-26 with digits offset to start at one.
Excel Sheet Column Title is the inverse divide loop, but bijective. Because there is no zero digit, you decrement by one before taking the remainder and quotient on each step, then prepend the resulting letter. Forgetting that single subtraction is the classic bug.
Key takeaways
- Divide-by-radix peels digits off; multiply-by-radix builds value up โ pick the direction the problem asks for.
- Two's complement means hex/binary conversions act on bits, so mask and shift logically instead of testing for a sign.
- Bijective bases (no zero) need a minus-one correction inside the divide loop; standard bases do not.
- Both directions run in time proportional to the number of digits, with constant extra space.
Core idea: Read the letters left to right and keep folding them in with
num = num * 26 + value(letter), whereA=1 ... Z=26. It's positional notation โ just a base-26 system with no zero digit.
Problem, rephrased
You're building the address bar for a tiny spreadsheet app. Columns are labelled with letters the way Excel does it: the first column is A, the 26th is Z, the 27th is AA, the 28th is AB, and so on. Given a column label (a string of uppercase letters), tell me which column number it is โ counting from 1.
Think of it as the inverse of "name the 28th column" โ here you're handed the name and asked for the number.
| Input (label) | Output (number) | Why |
|---|---|---|
"A" |
1 |
first column |
"Z" |
26 |
last single letter |
"AA" |
27 |
one past Z |
"AB" |
28 |
one past AA |
"ZY" |
701 |
two letters |
"AAA" |
703 |
first triple |
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson