The Excel Sheet Column Title interview question is the reverse of the previous problem. Given an integer column number, you must return its corresponding Excel column title as a string. For example, 1 returns "A", 26 returns "Z", and 27 returns "AA".
This is a popular Math interview pattern used by Amazon and Meta. it tests your ability to handle "Base-26" encoding where there is no zero. Because the system uses 1-26 instead of 0-25, a standard modulo operator (n % 26) can return 0 for "Z", which requires careful adjustment. It tests your problem-solving skills for non-standard mathematical systems.
The solution uses Modular Arithmetic with a 1-indexed adjustment.
n > 0:
n by 1 (to make it 0-indexed: A=0, B=1, ..., Z=25).rem = n % 26.rem to a character (0 -> 'A', 1 -> 'B', etc.) and prepend it to the result.n: n = n / 26.n = 28
n = 28 - 1 = 27. 27 % 26 = 1. Character is 'B'. n = 27 / 26 = 1.n = 1 - 1 = 0. 0 % 26 = 0. Character is 'A'. n = 0 / 26 = 0.
Result (reversed): "AB".n = 701
n = 700. 700 % 26 = 25. Character 'Z'. n = 700 / 26 = 26.n = 25. 25 % 26 = 25. Character 'Z'. n = 25 / 26 = 0.
Wait, 701 should be "ZY". Let's re-trace:700 % 26 = 24. 'Y'. 700/26 = 26.25 % 26 = 25. 'Z'. 25/26 = 0.
Result: "ZY".n % 26 directly, which gives 0 for 26 (Z), making it difficult to map to 'Z' without special if conditions.(char)('A' + rem)).Whenever you work with a numbering system that lacks a zero digit (like Excel columns or certain tally systems), the easiest trick is to subtract 1 before every modulo and division step. This maps the range [1, 26] to [0, 25], which is what the modulo operator expects.