Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to multiply the result by base when exponent is odd.
DSA Typescript
if (exponent % 2 !== 0) { result = result [1] base; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Using division or subtraction which changes the value incorrectly.
✗ Incorrect
When exponent is odd, multiply the current result by the base to include the extra factor.
2fill in blank
mediumComplete the code to halve the exponent in each step.
DSA Typescript
exponent = exponent [1] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using normal division which results in float.
Using multiplication or subtraction which changes the exponent incorrectly.
✗ Incorrect
Halving the exponent is done by integer division by 2 to reduce the problem size.
3fill in blank
hardFix the error in updating base by squaring it.
DSA Typescript
base = base [1] base;Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Using division or subtraction which changes the value incorrectly.
✗ Incorrect
To square the base, multiply it by itself.
4fill in blank
hardFill both blanks to complete the while loop condition and the loop body update.
DSA Typescript
while (exponent [1] 0) { if (exponent % 2 !== 0) { result = result * base; } base = base * base; exponent = exponent [2] 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' for the condition causes an infinite loop.
Using '/' which may produce floating point results.
✗ Incorrect
The loop continues while exponent is greater than zero, and exponent is halved using right shift (integer division by 2).
5fill in blank
hardFill all three blanks to complete the fast exponentiation function.
DSA Typescript
function fastPower(base: number, exponent: number): number {
let result = 1;
while (exponent [1] 0) {
if (exponent % 2 !== 0) {
result = result [2] base;
}
base = base [3] base;
exponent = Math.floor(exponent / 2);
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Using wrong loop condition causing infinite or no loops.
✗ Incorrect
The loop runs while exponent is greater than zero, multiply result by base when exponent is odd, and square the base each iteration.
