Challenge - 5 Problems
Fast Power Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Fast Exponentiation Function
What is the output of the following Python code that calculates 3 to the power 5 using fast exponentiation?
DSA Python
def fast_power(base, exponent): result = 1 while exponent > 0: if exponent % 2 == 1: result *= base base *= base exponent //= 2 return result print(fast_power(3, 5))
Attempts:
2 left
💡 Hint
Remember that 3^5 means 3 multiplied by itself 5 times.
✗ Incorrect
The function uses fast exponentiation to compute 3^5 = 243 efficiently by squaring the base and reducing the exponent by half each iteration.
❓ Predict Output
intermediate2:00remaining
Result of Fast Power with Zero Exponent
What will be the output of this code when calculating 10 to the power 0 using fast exponentiation?
DSA Python
def fast_power(base, exponent): result = 1 while exponent > 0: if exponent % 2 == 1: result *= base base *= base exponent //= 2 return result print(fast_power(10, 0))
Attempts:
2 left
💡 Hint
Any number to the power 0 is always?
✗ Incorrect
By definition, any number raised to the power 0 is 1. The function returns 1 without entering the loop.
❓ Predict Output
advanced2:00remaining
Output of Fast Power with Negative Exponent
What will the following code output when calculating 2 to the power -3 using the given fast exponentiation function?
DSA Python
def fast_power(base, exponent): result = 1 while exponent > 0: if exponent % 2 == 1: result *= base base *= base exponent //= 2 return result print(fast_power(2, -3))
Attempts:
2 left
💡 Hint
Check how the function handles negative exponents.
✗ Incorrect
The function only works for non-negative exponents. Since exponent is negative, the while loop never runs and returns 1.
🧠 Conceptual
advanced2:00remaining
Time Complexity of Fast Exponentiation
What is the time complexity of the fast exponentiation algorithm that calculates base^exponent by repeatedly squaring the base and halving the exponent?
Attempts:
2 left
💡 Hint
Think about how many times you can halve the exponent before it reaches zero.
✗ Incorrect
Each iteration halves the exponent, so the number of steps is proportional to log base 2 of the exponent.
🔧 Debug
expert3:00remaining
Identify the Bug in Fast Exponentiation Code
The following code is intended to compute base^exponent using fast exponentiation. What is the bug that causes incorrect results for odd exponents?
DSA Python
def fast_power(base, exponent): result = 1 while exponent > 0: if exponent % 2 == 1: result = base * base base = base * base exponent = exponent // 2 return result print(fast_power(2, 3))
Attempts:
2 left
💡 Hint
Check how the result variable is updated inside the loop.
✗ Incorrect
The code assigns result = base * base instead of multiplying result by base when exponent is odd, causing wrong output.