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: It's base-26 with no zero digit, so subtract 1 before each
% 26to fold 1..26 into 0..25, then map to 'A'..'Z'.
Problem, rephrased
Imagine you're building a tiny "ticket label" generator. The very first ticket prints A, the next B, โฆ the 26th Z. Then โ instead of rolling over to a two-character code with a leading zero like a clock would โ the 27th ticket prints AA, the 28th AB, and so on. There is no "ticket 0" and no label like "A0"; the alphabet simply is the digit set, all 26 letters, every one of them a real digit.
Your job: given a 1-based position n, return the label that lands at that position.
n (input) |
Label (output) | Why |
|---|---|---|
| 1 | "A" |
first letter |
| 26 | "Z" |
last single letter โ not a rollover |
| 27 | "AA" |
first two-letter label |
| 28 | "AB" |
|
| 52 | "AZ" |
26 + 26 |
| 701 | "ZY" |
|
| 702 | "ZZ" |
last two-letter label |
| 703 | "AAA" |
first three-letter label |
This is the inverse of Excel Sheet Column Number (which turns "AB" back into 28).
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