Magicsheet logo

Excel Sheet Column Title

Easy
38.7%
Updated 6/1/2025

Excel Sheet Column Title

What is this problem about?

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".

Why is this asked in interviews?

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.

Algorithmic pattern used

The solution uses Modular Arithmetic with a 1-indexed adjustment.

  1. While n > 0:
    • Decrement n by 1 (to make it 0-indexed: A=0, B=1, ..., Z=25).
    • Find the remainder: rem = n % 26.
    • Convert rem to a character (0 -> 'A', 1 -> 'B', etc.) and prepend it to the result.
    • Update n: n = n / 26.
  2. Return the result string.

Example explanation

n = 28

  1. n = 28 - 1 = 27. 27 % 26 = 1. Character is 'B'. n = 27 / 26 = 1.
  2. n = 1 - 1 = 0. 0 % 26 = 0. Character is 'A'. n = 0 / 26 = 0. Result (reversed): "AB".

n = 701

  1. n = 700. 700 % 26 = 25. Character 'Z'. n = 700 / 26 = 26.
  2. n = 25. 25 % 26 = 25. Character 'Z'. n = 25 / 26 = 0. Wait, 701 should be "ZY". Let's re-trace:
  3. 701 -> 700. 700 % 26 = 24. 'Y'. 700/26 = 26.
  4. 26 -> 25. 25 % 26 = 25. 'Z'. 25/26 = 0. Result: "ZY".

Common mistakes candidates make

  • Forgetting to subtract 1: Using n % 26 directly, which gives 0 for 26 (Z), making it difficult to map to 'Z' without special if conditions.
  • Prepend vs Append: Appending characters and forgetting to reverse the string at the end.
  • Off-by-one: Mistakes in character conversion math (e.g., (char)('A' + rem)).

Interview preparation tip

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.

Similar Questions