Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Fast Exponentiation 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 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;
}

#include <stdio.h>
int main() {
    printf("%d", fastPower(3, 5));
    return 0;
}
A15
B125
C243
D81
Attempts:
2 left
💡 Hint
Recall 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 snippet when calculating 5 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;
}

#include <stdio.h>
int main() {
    printf("%d", fastPower(5, 0));
    return 0;
}
A0
B1
C5
DUndefined behavior
Attempts:
2 left
💡 Hint
Any number raised to the power 0 is 1.
🔧 Debug
advanced
2:00remaining
Identify the Error in Fast Exponentiation Code
What error will this code produce when compiled and run?
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;
}

#include <stdio.h>
int main() {
    printf("%d", fastPower(2, 10));
    return 0;
}
AOutputs 0
BCompilation error due to operator precedence
CInfinite loop
DOutputs 1024
Attempts:
2 left
💡 Hint
Check operator precedence in the condition inside the if statement.
❓ Predict Output
advanced
2:00remaining
Output of Recursive Fast Exponentiation
What is the output of this recursive fast exponentiation function when calculating 2 raised to the power 6?
DSA C
int fastPower(int base, int exp) {
    if (exp == 0) return 1;
    int half = fastPower(base, exp / 2);
    if (exp % 2 == 0) {
        return half * half;
    } else {
        return half * half * base;
    }
}

#include <stdio.h>
int main() {
    printf("%d", fastPower(2, 6));
    return 0;
}
A64
B8
C16
D32
Attempts:
2 left
💡 Hint
2^6 means 2 multiplied by itself 6 times.
❓ Predict Output
expert
2:00remaining
Output of Fast Exponentiation with Negative Exponent Handling
What is the output of this modified fast exponentiation function that handles negative exponents by returning 0 for negative inputs, when called with base=2 and exp=-3?
DSA C
int fastPower(int base, int exp) {
    if (exp < 0) return 0;
    int result = 1;
    while (exp > 0) {
        if (exp % 2 == 1) {
            result *= base;
        }
        base *= base;
        exp /= 2;
    }
    return result;
}

#include <stdio.h>
int main() {
    printf("%d", fastPower(2, -3));
    return 0;
}
A0
B1
C-8
DUndefined behavior
Attempts:
2 left
💡 Hint
The function returns 0 immediately for negative exponents.

Practice

(1/5)
1. What is the main advantage of using Fast Exponentiation Power in Log N over the simple repeated multiplication method?
easy
A. It reduces the time complexity from O(n) to O(log n)
B. It uses less memory than simple multiplication
C. It works only for positive exponents
D. It avoids using multiplication altogether

Solution

  1. Step 1: Understand simple exponentiation time

    Simple repeated multiplication multiplies the base n times, so it takes O(n) time.
  2. Step 2: Understand fast exponentiation time

    Fast exponentiation uses squaring and divides the problem size by 2 each step, so it takes O(log n) time.
  3. Final Answer:

    It reduces the time complexity from O(n) to O(log n) -> Option A
  4. Quick Check:

    Fast exponentiation = O(log n) [OK]
Hint: Remember: divide exponent by 2 each step for speed [OK]
Common Mistakes:
  • Thinking it avoids multiplication
  • Confusing time complexity with memory usage
  • Assuming it only works for positive exponents
2. Which of the following is the correct C syntax to calculate pow(base, exponent) using fast exponentiation recursively?
easy
A. int fastPow(int base, int exp) { if (exp == 0) return 1; int half = fastPow(base, exp / 2); if (exp % 2 == 0) return half * half; else return base * half * half; }
B. int fastPow(int base, int exp) { int result = 1; for (int i = 0; i < exp; i++) result *= base; return result; }
C. int fastPow(int base, int exp) { if (exp == 1) return base; return base * fastPow(base, exp - 1); }
D. int fastPow(int base, int exp) { return base ^ exp; }

Solution

  1. Step 1: Identify recursive fast exponentiation

    int fastPow(int base, int exp) { if (exp == 0) return 1; int half = fastPow(base, exp / 2); if (exp % 2 == 0) return half * half; else return base * half * half; } uses recursion, divides exponent by 2, and squares the half result, matching fast exponentiation logic.
  2. Step 2: Check other options

    int fastPow(int base, int exp) { int result = 1; for (int i = 0; i < exp; i++) result *= base; return result; } is simple loop (not fast). int fastPow(int base, int exp) { if (exp == 1) return base; return base * fastPow(base, exp - 1); } is simple recursion without divide and conquer. int fastPow(int base, int exp) { return base ^ exp; } uses bitwise XOR, not power.
  3. Final Answer:

    int fastPow(int base, int exp) { if (exp == 0) return 1; int half = fastPow(base, exp / 2); if (exp % 2 == 0) return half * half; else return base * half * half; } -> Option A
  4. Quick Check:

    Recursive fast exponentiation = int fastPow(int base, int exp) { if (exp == 0) return 1; int half = fastPow(base, exp / 2); if (exp % 2 == 0) return half * half; else return base * half * half; } [OK]
Hint: Look for dividing exponent by 2 and squaring result [OK]
Common Mistakes:
  • Using simple loops instead of divide and conquer
  • Confusing bitwise XOR (^) with power
  • Missing base case for exponent 0
3. What is the output of the following C code snippet?
int fastPow(int base, int exp) {
    if (exp == 0) return 1;
    int half = fastPow(base, exp / 2);
    if (exp % 2 == 0) return half * half;
    else return base * half * half;
}

int main() {
    printf("%d", fastPow(2, 5));
    return 0;
}
medium
A. 25
B. 32
C. 10
D. 16

Solution

  1. Step 1: Trace fastPow(2, 5)

    fastPow(2,5) calls fastPow(2,2), which calls fastPow(2,1), which calls fastPow(2,0)=1. Then compute up: fastPow(2,1)=2*1*1=2, fastPow(2,2)=2*2=4, fastPow(2,5)=2*4*4=32.
  2. Step 2: Confirm output

    The final printed value is 32.
  3. Final Answer:

    32 -> Option B
  4. Quick Check:

    2^5 = 32 [OK]
Hint: Calculate powers by halving exponent and squaring [OK]
Common Mistakes:
  • Confusing multiplication order
  • Forgetting base case returns 1
  • Miscomputing odd exponent step
4. Identify the bug in the following fast exponentiation code and select the correct fix:
int fastPow(int base, int exp) {
    if (exp == 0) return 1;
    int half = fastPow(base, exp / 2);
    if (exp % 2 == 0) return half * half;
    else return base * half;
}
medium
A. Change exp / 2 to exp - 1;
B. Change base case to if (exp == 1) return base;
C. Change last line to return base * half * half;
D. No bug, code is correct

Solution

  1. Step 1: Analyze odd exponent case

    For odd exponent, result should be base * half * half, but code returns base * half only, missing one multiplication.
  2. Step 2: Fix the return statement

    Change last line to return base * half * half to correctly compute power for odd exponents.
  3. Final Answer:

    Change last line to return base * half * half; -> Option C
  4. Quick Check:

    Odd exponent step needs base * half * half [OK]
Hint: Remember odd exponent multiplies base once more [OK]
Common Mistakes:
  • Returning base * half instead of base * half * half
  • Changing base case incorrectly
  • Replacing exp / 2 with exp - 1
5. You want to compute pow(base, exponent) modulo m efficiently using fast exponentiation. Which modification should you apply to the standard fast exponentiation algorithm to handle large numbers without overflow?
hard
A. Use floating point arithmetic instead of integers
B. Only apply modulo at the final return value
C. Increase the size of integer variables to long long
D. Apply modulo operation after each multiplication step

Solution

  1. Step 1: Understand overflow risk

    Multiplying large numbers can overflow integer limits before modulo is applied.
  2. Step 2: Apply modulo after each multiplication

    To avoid overflow and keep numbers small, apply modulo after every multiplication step in fast exponentiation.
  3. Final Answer:

    Apply modulo operation after each multiplication step -> Option D
  4. Quick Check:

    Modulo after each multiply prevents overflow [OK]
Hint: Modulo after every multiply keeps numbers small [OK]
Common Mistakes:
  • Applying modulo only once at the end
  • Using floating point causing precision errors
  • Relying on bigger integer types without modulo
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard