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 Using Euclid's Algorithm
What is the output of the following JavaScript code that calculates the greatest common divisor (GCD) of two numbers using Euclid's algorithm?
DSA Javascript
function gcd(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
return a;
}
console.log(gcd(48, 18));Attempts:
2 left
💡 Hint
Recall that Euclid's algorithm finds the largest number that divides both numbers without remainder.
✗ Incorrect
Euclid's algorithm repeatedly replaces the larger number by the remainder of dividing the larger by the smaller until the remainder is zero. The last non-zero remainder is the GCD. For 48 and 18, the GCD is 6.
🧠 Conceptual
intermediateReason for Using Prime Factorization in DSA
Why is prime factorization important in solving some data structure and algorithm problems?
Attempts:
2 left
💡 Hint
Think about how breaking numbers into primes helps with divisibility problems.
✗ Incorrect
Prime factorization breaks numbers into their basic building blocks. This helps in calculating GCD and LCM efficiently, which are common in algorithm problems involving divisibility and optimization.
🔧 Debug
advancedIdentify the Error in Modular Exponentiation Code
What error does the following JavaScript code produce when calculating (base^exponent) % modulus?
DSA Javascript
function modExp(base, exponent, modulus) {
if (exponent === 0) return 1;
let half = modExp(base, Math.floor(exponent / 2), modulus);
let result = (half * half) % modulus;
if (exponent % 2 === 1) {
result = (result * base) % modulus;
}
return result;
}
console.log(modExp(2, 10, 1000));Attempts:
2 left
💡 Hint
Check the base case and recursive calls carefully.
✗ Incorrect
The code correctly implements modular exponentiation using recursion and returns 24 for 2^10 % 1000. There is no error or incorrect output.
🚀 Application
advancedUsing Number Theory to Detect Cycles in a Graph
How can number theory concepts help in detecting cycles in a graph represented by edges with weights?
Attempts:
2 left
💡 Hint
Think about how modular arithmetic can help identify repeating patterns or conditions.
✗ Incorrect
Modular arithmetic can be used to detect if the sum of weights in a cycle meets certain divisibility conditions, which helps in cycle detection algorithms in weighted graphs.
❓ Predict Output
expertOutput of Code Using Euler's Totient Function
What is the output of the following JavaScript code that calculates Euler's Totient function φ(n) for n=9?
DSA Javascript
function phi(n) {
let result = n;
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) {
while (n % i === 0) {
n = Math.floor(n / i);
}
result -= Math.floor(result / i);
}
}
if (n > 1) {
result -= Math.floor(result / n);
}
return result;
}
console.log(phi(9));Attempts:
2 left
💡 Hint
Euler's Totient function counts numbers less than n that are coprime to n.
✗ Incorrect
For n=9, which is 3^2, φ(9) = 9 * (1 - 1/3) = 6. The code correctly implements Euler's Totient function and outputs 6.
