0
0
DSA Pythonprogramming~20 mins

Fast Exponentiation Power in Log N in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fast Power Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
A125
Bnull (infinite loop)
C15
D243
Attempts:
2 left
💡 Hint
Remember that 3^5 means 3 multiplied by itself 5 times.
Predict Output
intermediate
2: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))
A10
B1
C0
DError
Attempts:
2 left
💡 Hint
Any number to the power 0 is always?
Predict Output
advanced
2: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))
A1 (function does not handle negative exponents, returns 1)
B1
C2
D0.125
Attempts:
2 left
💡 Hint
Check how the function handles negative exponents.
🧠 Conceptual
advanced
2: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?
AO(n^2), where n is the exponent
BO(n), where n is the exponent
CO(log n), where n is the exponent
DO(1), constant time
Attempts:
2 left
💡 Hint
Think about how many times you can halve the exponent before it reaches zero.
🔧 Debug
expert
3: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))
AThe result is overwritten instead of multiplied when exponent is odd
BThe base is not squared correctly
CThe exponent is not halved properly
DThe initial result should be 0, not 1
Attempts:
2 left
💡 Hint
Check how the result variable is updated inside the loop.