Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to multiply the result by x when n is odd.
DSA C
if (n % 2 != 0) { result = result [1] x; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' causes incorrect power calculation.
Using '/' or '-' leads to wrong results or runtime errors.
✗ Incorrect
When n is odd, multiply the current result by x to include the extra factor.
2fill in blank
mediumComplete the code to square x in each iteration.
DSA C
x = x [1] x; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds x to itself instead of squaring.
Using '/' or '-' causes wrong calculations.
✗ Incorrect
Squaring x means multiplying x by itself using '*'.
3fill in blank
hardFix the error in the loop condition to run while n is greater than zero.
DSA C
while (n [1] 0) { // loop body }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes the loop to never run if n starts positive.
Using '==' runs loop only when n is zero, which is incorrect.
✗ Incorrect
The loop should continue while n is greater than zero to process all bits.
4fill in blank
hardFill both blanks to correctly halve n and update the loop.
DSA C
n = n [1] 1; // loop continues while n [2] 0
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' for halving is correct but slower than '>>'.
Using '<' in loop condition causes wrong loop behavior.
✗ Incorrect
Use right shift '>>' to halve n efficiently and '>' to continue loop while n is positive.
5fill in blank
hardFill all three blanks to complete the fast exponentiation function.
DSA C
int fastPower(int x, int n) {
int result = 1;
while (n [1] 0) {
if (n & 1) {
result = result [2] x;
}
x = x [3] x;
n = n >> 1;
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '-' instead of '*' breaks power calculation.
Wrong loop condition causes infinite or no loops.
✗ Incorrect
Loop while n > 0, multiply result by x when n is odd, and square x each iteration.
