Skip to main content

168. Excel Sheet Column Title

easyAsked at Microsoft

Excel Sheet Column Title is Microsoft's deliberately-off-by-one base-26 puzzle. Excel columns A..Z, AA..AZ, BA.. are 1-indexed with no zero digit, so a vanilla base-26 conversion is wrong. The fix is subtracting one before every modulo.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Microsoft loops.

  • Glassdoor (2026-Q1)Microsoft Office/Excel team onsite reports list this and its inverse (LC 171) as the canonical column-conversion warm-up.
  • Blind (2025-11)Microsoft new-grad and L60 reports include Excel Sheet Column Title as a recurring easy.

Problem

Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet. For example, A -> 1, B -> 2, C -> 3, ..., Z -> 26, AA -> 27, AB -> 28, etc.

Constraints

  • 1 <= columnNumber <= 2^31 - 1

Examples

Example 1

Input
columnNumber = 1
Output
"A"

Example 2

Input
columnNumber = 28
Output
"AB"

Example 3

Input
columnNumber = 701
Output
"ZY"

Approaches

1. Subtract-then-modulo (optimal)

Loop while n > 0: subtract 1 (to fix 1-indexing), take mod 26 to get the next character, append, divide by 26. Reverse at the end.

Time
O(log_26 n)
Space
O(log_26 n) for the output
function convertToTitle(n) {
  const chars = [];
  while (n > 0) {
    n--;
    chars.push(String.fromCharCode('A'.charCodeAt(0) + (n % 26)));
    n = Math.floor(n / 26);
  }
  return chars.reverse().join('');
}

Tradeoff: The n-- BEFORE every modulo is the entire trick. Without it, n=26 gives mod=0 (which has no Excel letter) and the answer collapses to 'A' instead of 'Z'. The subtraction shifts the 1..26 range down to 0..25 so the modulo behaves like a true base.

2. Recursive (same trick)

Decrement n, recurse on Math.floor(n/26), append the current letter.

Time
O(log_26 n)
Space
O(log_26 n) stack
function convertToTitle(n) {
  if (n <= 0) return '';
  n--;
  return convertToTitle(Math.floor(n / 26)) + String.fromCharCode('A'.charCodeAt(0) + (n % 26));
}

Tradeoff: Same complexity. Microsoft will accept either; the iterative version is preferred because the recursive base case is easy to miswrite (return '' on n <= 0 only AFTER the decrement).

Microsoft-specific tips

Microsoft is deliberately testing whether you spot the off-by-one. Trace n=26 on the whiteboard: vanilla base-26 says 26 / 26 = 1, 26 % 26 = 0, which maps to '?A' — wrong. Then trace it with the n-- fix: 25 / 26 = 0, 25 % 26 = 25, which maps to 'Z'. That before-and-after on the whiteboard is the answer to the interview.

Common mistakes

  • Skipping the n-- — produces wrong output at n=26, n=52, n=702, every multiple of 26.
  • Doing the n-- AFTER the modulo — same wrong answer.
  • Forgetting to reverse the result in the iterative version.

Follow-up questions

An interviewer at Microsoft may pivot to one of these next:

  • Excel Sheet Column Number (LC 171) — inverse direction, the easier of the pair.
  • Any base-conversion problem with non-zero-indexed digits.
  • What if the column letters were arbitrary symbols instead of A-Z?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is this not just base-26?

Base-26 uses digits 0..25. Excel columns use 1..26 with no 'zero column.' That extra unit shifts the representation, and the n-- compensates for it.

What's the largest column number we need to handle?

2^31 - 1 per the constraints — which produces a ~7-character column. The log_26 of 2^31 is about 6.6, so the answer is at most 7 letters.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Excel Sheet Column Title and other Microsoft interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →