Challenge - 5 Problems
Fast Exponentiation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of fast exponentiation with positive exponent
What is the output of the following Java code that calculates 3 to the power 5 using fast exponentiation?
DSA Java
public class Power { public static long fastPow(long base, long exp) { long result = 1; while (exp > 0) { if ((exp & 1) == 1) { result *= base; } base *= base; exp >>= 1; } return result; } public static void main(String[] args) { System.out.println(fastPow(3, 5)); } }
Attempts:
2 left
💡 Hint
Recall that 3^5 means 3 multiplied by itself 5 times.
✗ Incorrect
3^5 = 3 * 3 * 3 * 3 * 3 = 243. The fast exponentiation method efficiently calculates this by squaring and multiplying selectively.
❓ Predict Output
intermediateOutput when exponent is zero
What will be printed by this Java code when calculating 10 to the power 0 using fast exponentiation?
DSA Java
public class PowerZero { public static long fastPow(long base, long exp) { long result = 1; while (exp > 0) { if ((exp & 1) == 1) { result *= base; } base *= base; exp >>= 1; } return result; } public static void main(String[] args) { System.out.println(fastPow(10, 0)); } }
Attempts:
2 left
💡 Hint
Any number raised to the power 0 is 1.
✗ Incorrect
By definition, any non-zero number to the power 0 equals 1. The code returns 1 immediately since the loop does not run.
❓ Predict Output
advancedOutput of fast exponentiation with negative exponent (incorrect handling)
What will be the output of this Java code when calculating 2 to the power -3 using the given fast exponentiation method?
DSA Java
public class PowerNegative { public static long fastPow(long base, long exp) { long result = 1; while (exp > 0) { if ((exp & 1) == 1) { result *= base; } base *= base; exp >>= 1; } return result; } public static void main(String[] args) { System.out.println(fastPow(2, -3)); } }
Attempts:
2 left
💡 Hint
Check how the while loop condition handles negative exponents.
✗ Incorrect
Since exp is negative, the condition exp > 0 is false initially, so the loop never runs and result remains 1.
🧠 Conceptual
advancedTime complexity of fast exponentiation
What is the time complexity of the fast exponentiation algorithm that uses repeated squaring and bitwise operations to compute base^exp?
Attempts:
2 left
💡 Hint
Consider how many times the exponent can be divided by 2.
✗ Incorrect
The algorithm reduces the exponent by half each iteration using bit shifts, so it runs in logarithmic time relative to the exponent.
🔧 Debug
expertIdentify the bug in fast exponentiation with modulo
This Java code attempts to compute (base^exp) % mod using fast exponentiation. Which option correctly identifies the bug causing wrong results?
DSA Java
public class PowerMod { public static long fastPowMod(long base, long exp, long mod) { long result = 1; base = base % mod; while (exp > 0) { if ((exp & 1) == 1) { result = (result * base) % mod; } base = (base * base) % mod; exp >>= 1; } return result; } public static void main(String[] args) { System.out.println(fastPowMod(2, 10, 1000)); } }
Attempts:
2 left
💡 Hint
Consider how multiplication of large numbers can overflow before modulo is applied.
✗ Incorrect
Multiplying two long values can overflow before modulo is applied, causing incorrect results. Using a safe multiplication method or BigInteger is needed.
