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.
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) Initialize `exponent` to 0, representing the smallest exponent.
2) Initialize `power` to 1, which is base^0.
3) 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.
4) Once the loop exits, `exponent` will be one less than the number of successful multiplications, so return it.
⚠️ Common Mistakes
base
is 1 or limit
is less than base
.power
or misplacing the increment of exponent
.def find_highest_exponent(base, limit):
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