Excel Sheet Column Title - 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
  • ...

Input & Output

Example 1 — Single Letter
$ Input: columnNumber = 1
Output: "A"
💡 Note: The first column in Excel is labeled 'A'
Example 2 — Double Letter Start
$ Input: columnNumber = 28
Output: "AB"
💡 Note: After Z (26), comes AA (27), then AB (28)
Example 3 — Large Number
$ Input: columnNumber = 701
Output: "ZY"
💡 Note: 701 converts to ZY using base-26 conversion with adjustment

Constraints

  • 1 ≤ columnNumber ≤ 231 - 1

Visualization

Tap to expand
Excel Sheet Column Title INPUT A B C 1 2 3 Column Mapping: A=1, B=2, ... Z=26 AA=27, AB=28, ... columnNumber 1 Base-26 system (1-indexed, not 0) ALGORITHM STEPS 1 Initialize result = "" (empty string) 2 While n > 0 Process each digit 3 Adjust for 1-index n = n - 1 remainder = n % 26 4 Get character char = 'A' + remainder prepend to result For n=1: n-1 = 0 0 % 26 = 0 'A' + 0 = 'A' FINAL RESULT A B C Column 1 Output: "A" OK - Verified Key Insight: This is a base-26 conversion, but with 1-indexed digits (A=1, not A=0). We must subtract 1 before each modulo operation to handle this offset. Build the string from right to left, then reverse or prepend characters. Time: O(log n), Space: O(1). TutorialsPoint - Excel Sheet Column Title | Optimal Solution
Asked in
Microsoft 35 Google 25 Amazon 20 Apple 15
89.2K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen