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 Function
What is the output of the following C# code that calculates 3 to the power 5 using fast exponentiation?
DSA C#
using System; class Program { static long FastPower(long x, long n) { if (n == 0) return 1; long half = FastPower(x, n / 2); if (n % 2 == 0) return half * half; else return half * half * x; } static void Main() { Console.WriteLine(FastPower(3, 5)); } }
Attempts:
2 left
💡 Hint
Recall that 3^5 means 3 multiplied by itself 5 times.
✗ Incorrect
The function uses recursion to compute powers efficiently by dividing the exponent by 2 each time. 3^5 = 243.
❓ Predict Output
intermediateResult of Fast Exponentiation with Zero Exponent
What will be printed when running this C# code that calculates 10 to the power 0 using fast exponentiation?
DSA C#
using System; class Program { static long FastPower(long x, long n) { if (n == 0) return 1; long half = FastPower(x, n / 2); if (n % 2 == 0) return half * half; else return half * half * x; } static void Main() { Console.WriteLine(FastPower(10, 0)); } }
Attempts:
2 left
💡 Hint
Any number to the power 0 is 1.
✗ Incorrect
The base case returns 1 when exponent is zero, so output is 1.
❓ Predict Output
advancedOutput of Fast Exponentiation with Negative Exponent
What happens when you run this C# code that tries to compute 2 to the power -3 using the given fast exponentiation function?
DSA C#
using System; class Program { static double FastPower(double x, int n) { if (n == 0) return 1; double half = FastPower(x, n / 2); if (n % 2 == 0) return half * half; else if (n > 0) return half * half * x; else return (half * half) / x; } static void Main() { Console.WriteLine(FastPower(2, -3)); } }
Attempts:
2 left
💡 Hint
Negative powers mean 1 divided by the positive power.
✗ Incorrect
The function handles negative exponents by dividing instead of multiplying. 2^-3 = 1/(2^3) = 0.125.
🧠 Conceptual
advancedTime Complexity of Fast Exponentiation
What is the time complexity of the fast exponentiation algorithm that computes x^n by recursively dividing the exponent by 2?
Attempts:
2 left
💡 Hint
Each recursive call halves the exponent.
✗ Incorrect
Because the exponent is divided by 2 each time, the number of calls is proportional to log n.
🔧 Debug
expertIdentify the Bug in Fast Exponentiation Code
What error will this C# code produce when computing 2 to the power 10 using fast exponentiation?
DSA C#
using System; class Program { static long FastPower(long x, long n) { if (n == 0) return 1; long half = FastPower(x, n / 2); if (n % 2 == 0) return half * half; else return half * half * x; } static void Main() { Console.WriteLine(FastPower(2, 10)); } }
Attempts:
2 left
💡 Hint
Check if the recursion base case and multiplication are correct.
✗ Incorrect
The code correctly computes 2^10 = 1024 without errors or overflow for this input.
