Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to multiply the result by base when exponent is odd.
DSA Rust
if n % 2 == 1 { result = result [1] base; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of * causes wrong calculation.
Using / or - causes runtime errors or wrong results.
✗ Incorrect
When exponent is odd, multiply the current result by base using the * operator.
2fill in blank
mediumComplete the code to halve the exponent n using integer division.
DSA Rust
n = n [1] 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using * or + changes the exponent incorrectly.
Using - reduces exponent incorrectly.
✗ Incorrect
Halving the exponent is done by integer division using / operator in Rust.
3fill in blank
hardFix the error in squaring the base inside the loop.
DSA Rust
base = base [1] base;Attempts:
3 left
💡 Hint
Common Mistakes
Using + or - results in wrong base update.
Using / causes runtime errors.
✗ Incorrect
To square the base, multiply it by itself using * operator.
4fill in blank
hardFill both blanks to complete the while loop condition and the function return statement.
DSA Rust
while [1] > 0 { // loop body } return [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using base in loop condition causes infinite loop.
Returning base instead of result gives wrong output.
✗ Incorrect
The loop runs while n > 0, and the function returns the accumulated result.
5fill in blank
hardFill all three blanks to complete the fast exponentiation function.
DSA Rust
fn fast_pow(mut base: i32, mut [1]: i32) -> i32 { let mut [2] = 1; while [1] > 0 { if [1] % 2 == 1 { [2] = [2] * base; } base = base * base; [1] = [1] / 2; } [2] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names causes errors.
Not initializing result to 1 causes wrong output.
✗ Incorrect
The exponent variable is named 'exponent', result is stored in 'result', and the loop uses 'exponent'.
