Code Room
CodingHardcod-g183
Subject Number theoryLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given an integer n (3 <= n <= 10^18) provided as a Python int, find the smallest base k >= 2 such that the representation of n in base k consists entirely of 1s, i.e. n = 1 + k + k^2 + ... + k^m for some m >= 1. Return k. For example n = 13 = 111 in base 3, so the answer is 3. Every n has the trivial representation 11 in base n-1, so an answer always exists.

Implement
smallest_good_base(n: int) → int
Examples
in[13]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.