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 C++ code that calculates 3 raised to the power 5 using fast exponentiation?
DSA C++
int fastPower(int base, int exp) { int result = 1; while (exp > 0) { if (exp % 2 == 1) { result *= base; } base *= base; exp /= 2; } return result; } int main() { int res = fastPower(3, 5); std::cout << res << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Recall that 3^5 = 3 * 3 * 3 * 3 * 3
✗ Incorrect
The fast exponentiation correctly computes 3^5 = 243 by multiplying base when exponent bit is set and squaring base each iteration.
❓ Predict Output
intermediateOutput when exponent is zero
What will the following code print when calculating 10 raised to the power 0 using fast exponentiation?
DSA C++
int fastPower(int base, int exp) { int result = 1; while (exp > 0) { if (exp % 2 == 1) { result *= base; } base *= base; exp /= 2; } return result; } int main() { int res = fastPower(10, 0); std::cout << res << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Any number raised to the power 0 is 1.
✗ Incorrect
The function initializes result to 1 and since exponent is zero, the loop does not run, so it returns 1.
❓ Predict Output
advancedOutput of fast exponentiation with negative exponent (integer version)
What will the following code output when calculating 2 raised to the power -3 using the given fast exponentiation function?
DSA C++
int fastPower(int base, int exp) { int result = 1; while (exp > 0) { if (exp % 2 == 1) { result *= base; } base *= base; exp /= 2; } return result; } int main() { int res = fastPower(2, -3); std::cout << res << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Consider how the while loop condition handles negative exponents.
✗ Incorrect
The loop condition exp > 0 is false initially for negative exponent, so the loop never runs and the function returns 1, which is incorrect for negative powers. This is a logical error.
🧠 Conceptual
advancedTime complexity of fast exponentiation
What is the time complexity of the fast exponentiation algorithm that calculates base^exp by repeatedly squaring and multiplying when needed?
Attempts:
2 left
💡 Hint
Think about how the exponent is reduced each step.
✗ Incorrect
The exponent is divided by 2 each iteration, so the number of steps is proportional to log base 2 of the exponent.
🔧 Debug
expertIdentify the bug in this fast exponentiation code
What error will this C++ code produce when trying to compute 2^10 using fast exponentiation?
DSA C++
int fastPower(int base, int exp) { int result = 1; while (exp > 0) { if ((exp & 1) = 1) { result *= base; } base *= base; exp >>= 1; } return result; } int main() { int res = fastPower(2, 10); std::cout << res << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check the condition inside the if statement carefully.
✗ Incorrect
The expression 'exp & 1 = 1' is parsed as (exp & 1) = 1 because the bitwise & operator has higher precedence than assignment =. However, (exp & 1) is an rvalue and cannot be assigned to, resulting in a compilation error: 'lvalue required as left operand of assignment'. The intended condition should be 'exp & 1 == 1'.
