Challenge - 5 Problems
Math & Number Theory Mastery in DSA
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of GCD Calculation in Rust
What is the output of this Rust code that calculates the Greatest Common Divisor (GCD) of two numbers using Euclid's algorithm?
DSA Rust
fn gcd(mut a: u32, mut b: u32) -> u32 {
while b != 0 {
let temp = b;
b = a % b;
a = temp;
}
a
}
fn main() {
let result = gcd(48, 18);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Recall that GCD is the largest number dividing both without remainder.
✗ Incorrect
The Euclidean algorithm repeatedly replaces the larger number by the remainder until the remainder is zero. The last non-zero remainder is the GCD. For 48 and 18, the GCD is 6.
🧠 Conceptual
intermediateWhy Use Modular Arithmetic in DSA?
Why is modular arithmetic commonly used in data structures and algorithms problems?
Attempts:
2 left
💡 Hint
Think about how computers handle very large numbers.
✗ Incorrect
Modular arithmetic keeps numbers within a fixed range, preventing overflow and making calculations manageable, especially in hashing and combinatorics.
❓ Predict Output
advancedOutput of Prime Check Function
What is the output of this Rust code that checks if a number is prime?
DSA Rust
fn is_prime(n: u32) -> bool { if n <= 1 { return false; } for i in 2..=((n as f64).sqrt() as u32) { if n % i == 0 { return false; } } true } fn main() { let result = is_prime(29); println!("{}", result); }
Attempts:
2 left
💡 Hint
29 is a well-known prime number.
✗ Incorrect
The function correctly checks divisibility up to the square root of n. Since 29 has no divisors other than 1 and itself, it returns true.
🔧 Debug
advancedIdentify the Error in Modular Exponentiation
What error will this Rust code produce when calculating modular exponentiation?
DSA Rust
fn mod_exp(mut base: u64, mut exp: u64, modulus: u64) -> u64 {
let mut result = 1;
while exp > 0 {
if exp % 2 == 1 {
result = (result * base) % modulus;
}
base = (base * base) % modulus;
exp /= 2;
}
result
}
fn main() {
let val = mod_exp(2, 10, 1000);
println!("{}", val);
}Attempts:
2 left
💡 Hint
Check how the exponent is updated inside the loop.
✗ Incorrect
The line 'exp /= 1;' does not reduce exp, causing an infinite loop.
🚀 Application
expertNumber Theory in Hashing Algorithms
Which number theory concept is most important for designing a good hashing function to minimize collisions?
Attempts:
2 left
💡 Hint
Think about numbers that help distribute values evenly.
✗ Incorrect
Prime numbers help in spreading hash values uniformly, reducing collisions in hash tables.
