Bird
Raised Fist0
DSA Rustprogramming~10 mins

Modular Arithmetic Basics in DSA Rust - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to compute the remainder of 17 divided by 5 using modular arithmetic.

DSA Rust
let result = 17 [1] 5;
println!("{}", result);
Drag options to blanks, or click blank then click option'
A*
B/
C%
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using division operator / instead of modulo %.
Using multiplication or addition operators which do not give remainder.
2fill in blank
medium

Complete the code to compute (7 + 12) modulo 10.

DSA Rust
let sum = (7 + 12) [1] 10;
println!("{}", sum);
Drag options to blanks, or click blank then click option'
A/
B-
C*
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using division / instead of modulo %.
Using subtraction or multiplication operators incorrectly.
3fill in blank
hard

Fix the error in the code to correctly compute (8 * 9) modulo 7.

DSA Rust
let product = (8 [1] 9) % 7;
println!("{}", product);
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Using division which is not intended here.
4fill in blank
hard

Fill both blanks to create a function that returns (a + b) modulo m.

DSA Rust
fn mod_add(a: i32, b: i32, m: i32) -> i32 {
    (a [1] b) [2] m
}
Drag options to blanks, or click blank then click option'
A+
B%
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of operators.
Using multiplication instead of addition.
5fill in blank
hard

Fill all three blanks to create a function that returns (a * b + c) modulo m.

DSA Rust
fn mod_expr(a: i32, b: i32, c: i32, m: i32) -> i32 {
    (a [1] b [2] c) [3] m
}
Drag options to blanks, or click blank then click option'
A*
B+
C%
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Changing the order of operations incorrectly.
Using subtraction or division instead of addition or multiplication.

Practice

(1/5)
1. What is the result of 7 % 3 in modular arithmetic?
easy
A. 2
B. 1
C. 3
D. 0

Solution

  1. Step 1: Understand the modulo operation

    The modulo operator (%) gives the remainder after division of the first number by the second.
  2. Step 2: Calculate 7 modulo 3

    7 divided by 3 is 2 with a remainder of 1, so 7 % 3 = 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    7 % 3 = 1 [OK]
Hint: Modulo gives remainder after division [OK]
Common Mistakes:
  • Confusing modulo with division
  • Using 3 instead of remainder
  • Thinking modulo returns quotient
2. Which of the following is the correct Rust syntax to compute the remainder of 15 divided by 4?
easy
A. let result = 15 / 4;
B. let result = 15 mod 4;
C. let result = 15 % 4;
D. let result = 15 rem 4;

Solution

  1. Step 1: Recall Rust modulo syntax

    In Rust, the modulo operator is the percent sign (%), not 'mod' or 'rem'.
  2. Step 2: Identify correct syntax

    let result = 15 % 4; uses % correctly: let result = 15 % 4;
  3. Final Answer:

    let result = 15 % 4; -> Option C
  4. Quick Check:

    Rust uses % for modulo [OK]
Hint: Use % operator for modulo in Rust [OK]
Common Mistakes:
  • Using 'mod' keyword incorrectly
  • Using division operator instead of modulo
  • Using 'rem' which is invalid syntax
3. What is the output of the following Rust code?
fn main() {
    let a = 10;
    let b = 6;
    let c = (a + b) % 7;
    println!("{}", c);
}
medium
A. 4
B. 2
C. 3
D. 1

Solution

  1. Step 1: Calculate sum inside parentheses

    a + b = 10 + 6 = 16.
  2. Step 2: Compute modulo 7 of the sum

    16 % 7 = 2 (because 7 * 2 = 14, remainder 2).
  3. Step 3: Check printed output

    The code prints the value of c, which is 2.
  4. Final Answer:

    2 -> Option B
  5. Quick Check:

    (10 + 6) % 7 = 2 [OK]
Hint: Calculate sum first, then modulo [OK]
Common Mistakes:
  • Modulo before addition
  • Wrong remainder calculation
  • Confusing modulo with division
4. Identify the error in this Rust code snippet that tries to compute modulo:
fn main() {
    let x = 20;
    let y = 0;
    let z = x % y;
    println!("{}", z);
}
medium
A. Division by zero error at runtime
B. Syntax error: % operator not allowed
C. Variable y is not initialized
D. No error, code runs fine

Solution

  1. Step 1: Analyze modulo operation with zero divisor

    Modulo by zero is undefined and causes a runtime error in Rust.
  2. Step 2: Identify error type

    The code will compile but panic at runtime due to division by zero.
  3. Final Answer:

    Division by zero error at runtime -> Option A
  4. Quick Check:

    Modulo by zero causes runtime panic [OK]
Hint: Never modulo by zero; causes runtime panic [OK]
Common Mistakes:
  • Thinking it's a syntax error
  • Ignoring zero divisor
  • Assuming code runs fine
5. You want to compute (a * b) % m for very large integers a and b in Rust without overflow. Which approach is best?
hard
A. Compute a * b first, then modulo m
B. Convert numbers to strings and then modulo
C. Use floating point multiplication and then modulo
D. Use modular multiplication: ((a % m) * (b % m)) % m

Solution

  1. Step 1: Understand overflow risk

    Multiplying large integers directly can overflow before modulo is applied.
  2. Step 2: Apply modular multiplication property

    Using ((a % m) * (b % m)) % m keeps intermediate values small and avoids overflow.
  3. Step 3: Evaluate other options

    Compute a * b first, then modulo m risks overflow; options B and C are invalid or inefficient.
  4. Final Answer:

    Use modular multiplication: ((a % m) * (b % m)) % m -> Option D
  5. Quick Check:

    (a * b) % m = ((a % m) * (b % m)) % m [OK]
Hint: Apply modulo before multiplication to avoid overflow [OK]
Common Mistakes:
  • Multiplying before modulo causing overflow
  • Using floats incorrectly
  • String conversion misuse
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