Challenge - 5 Problems
Fast Power 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 JavaScript function call
fastPower(2, 10)?DSA Javascript
function fastPower(base, exponent) {
if (exponent === 0) return 1;
let half = fastPower(base, Math.floor(exponent / 2));
if (exponent % 2 === 0) {
return half * half;
} else {
return half * half * base;
}
}
console.log(fastPower(2, 10));Attempts:
2 left
💡 Hint
Remember that 2 to the power of 10 is 1024.
✗ Incorrect
The function uses fast exponentiation by recursively halving the exponent. 2^10 equals 1024.
❓ Predict Output
intermediateOutput for Odd Exponent
What is the output of
fastPower(3, 5) using the same function as above?DSA Javascript
function fastPower(base, exponent) {
if (exponent === 0) return 1;
let half = fastPower(base, Math.floor(exponent / 2));
if (exponent % 2 === 0) {
return half * half;
} else {
return half * half * base;
}
}
console.log(fastPower(3, 5));Attempts:
2 left
💡 Hint
3^5 means 3 multiplied by itself 5 times.
✗ Incorrect
3^5 = 3*3*3*3*3 = 243. The function handles odd exponents by multiplying an extra base.
🔧 Debug
advancedIdentify the Error in Fast Exponentiation
What error will occur when running this code snippet?
DSA Javascript
function fastPower(base, exponent) {
if (exponent === 0) return 1;
let half = fastPower(base, exponent / 2);
if (exponent % 2 === 0) {
return half * half;
} else {
return half * half * base;
}
}
console.log(fastPower(2, 5));Attempts:
2 left
💡 Hint
Check how the exponent is divided in the recursive call.
✗ Incorrect
The function uses exponent / 2 without flooring, causing infinite recursion with fractional exponents.
❓ Predict Output
advancedOutput of Fast Exponentiation with Large Exponent
What is the output of
fastPower(5, 0)?DSA Javascript
function fastPower(base, exponent) {
if (exponent === 0) return 1;
let half = fastPower(base, Math.floor(exponent / 2));
if (exponent % 2 === 0) {
return half * half;
} else {
return half * half * base;
}
}
console.log(fastPower(5, 0));Attempts:
2 left
💡 Hint
Any number to the power of zero is 1.
✗ Incorrect
The base case returns 1 when exponent is zero, which is mathematically correct.
🧠 Conceptual
expertTime Complexity of Fast Exponentiation
What is the time complexity of the fast exponentiation algorithm implemented using recursion and halving the exponent each time?
Attempts:
2 left
💡 Hint
Each recursive call halves the exponent.
✗ Incorrect
The exponent is divided by 2 each recursive call, so the number of calls is proportional to log n.
