Bird
0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 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.