-
Notifications
You must be signed in to change notification settings - Fork 273
Highest Exponent
Mitchell Bryson edited this page Aug 5, 2026
·
6 revisions
TIP101 Unit 4 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- What if the base is 1?
- Every power of 1 is still 1, so no finite highest exponent exists. The function should reject this input (e.g. raise a
ValueError) rather than loop forever.
- Every power of 1 is still 1, so no finite highest exponent exists. The function should reject this input (e.g. raise a
- What if the limit is less than the base?
- The highest exponent should be 0, as any positive exponent would exceed the limit.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Start with the smallest exponent and multiply the base until the result exceeds the limit.
1) If `base` is 1, raise an error — every power of 1 is 1, so no highest exponent exists.
2) Initialize `exponent` to 0, representing the smallest exponent.
3) Initialize `power` to 1, which is base^0.
4) While multiplying the current `power` by the base stays within the `limit`:
a) Multiply `power` by `base` to get the next power.
b) Increment `exponent` by 1 to reflect the next higher power level.
5) Once the loop exits, `exponent` equals the number of successful multiplications — the largest `exponent` with base^exponent <= limit — so return it.- Forgetting to handle edge cases where
baseis 1 orlimitis less thanbase. - Incorrectly updating the
poweror misplacing the increment ofexponent.
def find_highest_exponent(base, limit):
if base == 1:
# Every power of 1 is 1, so no finite highest exponent exists
raise ValueError("base 1 has no highest exponent: 1**k equals 1 for every k")
exponent = 0 # Start with an exponent of 0
power = 1 # The result of base^exponent
while power * base <= limit:
power *= base
exponent += 1 # Increment the exponent each time the base is multiplied
return exponent