Data Structures & AlgorithmsModerateProduct Company SDE · Amazon/Microsoft-style OA

Problem: Return the excel-style column title for a column number (1 → A, 26 → Z, 27 → AA).

Problem: Return the excel-style column title for a column number (1 → A, 26 → Z, 27 → AA).
def title(n):
    s = ''
    while n:
        n -= 1
        s = chr(65 + n % 26) + s
        n //= 26
    return s
print(title(28), title(52), title(701), title(703))
What is printed?
  1. A. AB BZ ZY AAA
  2. B. AB AZ ZY AAA
  3. C. AB AZ ZZ AAA
  4. D. BB AZ ZY AAA

Correct answer: B. AB AZ ZY AAA

Bijective base 26 has no zero digit, so subtract 1 before each division. 52 = 1·26 + 26 → AZ (without the −1 you get 'B' followed by a broken digit). 701 → ZY, 703 → AAA. O(log₂₆ n).

More Coding Interview Problems questions

Exams that ask this topic