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?- A. AB BZ ZY AAA
- B. AB AZ ZY AAA
- C. AB AZ ZZ AAA
- 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
- Problem: Return the number formed by the most significant digit of the sum of digits repeatedly added until one digit remains (digital…
- Problem: Count pairs (i < j) with a[i] > a[j] (inversions) in an array of n = 2 × 10⁵ values. Which approach runs within a 1-second limit?
- Problem: Answer q = 10⁵ queries 'sum of a[l..r]' on a static array of n = 10⁵ values. Which approach is best?
- Problem: An array of n = 10⁵ values receives 10⁵ operations, each either 'add v to a[i]' or 'sum of a[l..r]'. Which data structure gives…
- Problem: n ≤ 18 cities, find the shortest route visiting every city exactly once (travelling salesman). Which approach fits?
- Problem: n = 10⁵ intervals; for each, count how many other intervals it overlaps. What is the best complexity?