The O(n) Club: Excel Sheet Column Title — The “A-Z” That Breaks Brains

The O(n) Club: Excel Sheet Column Title — The “A-Z” That Breaks Brains

⚡ TL;DR

Turn a number into Excel’s loopy column labels: 1 → “A”, 28 → “AB”, 702 → “ZZ”. Do yourself a favor—subtract one before mod 26 or your columns’ ancestors will haunt your code forever.

// Brute force (and slightly cursed)
public String convertToTitle(int n) {
    StringBuilder sb = new StringBuilder();
    while (n > 0) {
        int mod = n % 26;
        sb.append((char) ('A' + mod - 1)); // Oops! This bites for multiples of 26
        n /= 26;
    }
    return sb.reverse().toString();
}

🧠 How Devs Usually Mess This Up

Everyone rushes in thinking, “Hey, base-26 conversion!” Not so fast, champ. Excel’s system is bijective base-26, which skips zero like a party you weren’t invited to. That means you must subtract one before every modulo, or your columns will start with “@” or—worse—put the alphabet in the wrong places. And hey, if you never reverse your result, enjoy spelling “BA” when you really want “AB”. (Congratulations, you broke the spreadsheet.)

🔍 What’s Actually Going On

Excel column labels are a weird “no zero allowed” version of base-26. Imagine a vending machine that’s never heard of the number zero: it counts A=1, Z=26, then next jump is AA=27. Regular digit conversion won’t work here—the digits are all off by one! Subtract 1, grab a letter based on modulo 26, and keep dividing, working backwards through the title. Who needs intuitive math when you have Microsoft?

🛠️ PseudoCode

  • While your number is > 0:
    • Subtract one (n–). Welcome to real Excel math.
    • Take n % 26 for the rightmost letter’s index.
    • Add ‘A’ + index to your result.
    • Divide number by 26 (n /= 26).
    • Rinse and repeat. Build in reverse.
  • Reverse the accumulated characters at the end, unless chaos is your friend.
// Example for 28:
// Step 1: 28 - 1 = 27; 27 % 26 = 1 -> 'B'; 27 / 26 = 1
// Step 2: 1 - 1 = 0; 0 % 26 = 0 -> 'A'; 0 / 26 = 0
// So result is "BA", reverse to "AB"

💻 The Code

public String convertToTitle(int columnNumber) {
    StringBuilder sb = new StringBuilder();
    while (columnNumber > 0) {
        columnNumber--; // Don’t forget this; Excel never did
        int remainder = columnNumber % 26;
        char letter = (char) ('A' + remainder);
        sb.append(letter);
        columnNumber /= 26;
    }
    return sb.reverse().toString();
}

⚠️ Pitfalls, Traps & Runtime Smacks

  • Dodge the classic off-by-one bug: You MUST subtract 1 before % 26.
  • Reverse your string at the end, or get ready for backwards alphabet soup.
  • This system is zero-phobic: no digit ever means zero, so standard base math breaks here.
  • Time complexity: O(log N) — basically, how many letters your column has. Space: O(L) for a StringBuilder.

🧠 Interviewer Brain-Tattoo

“If you don’t subtract one, Excel columns will stare at you until you reverse your modulo sins.”

Previous Article

The O(n) Club: Expression Add Operators—The Recursion-Induced Headache Edition

Next Article

The O(n) Club: Sum of Distances in Tree — Two DFSes, No Tears