Challenge - 5 Problems
Number Theory Mastery in DSA
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of GCD Calculation in a Loop
What is the output of the following C code that calculates the GCD of pairs of numbers?
DSA C
#include <stdio.h> int gcd(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } int main() { int pairs[3][2] = {{12, 18}, {100, 80}, {7, 13}}; for (int i = 0; i < 3; i++) { printf("%d\n", gcd(pairs[i][0], pairs[i][1])); } return 0; }
Attempts:
2 left
💡 Hint
Recall the Euclidean algorithm for GCD and how modulo works.
✗ Incorrect
The gcd function uses the Euclidean algorithm. gcd(12,18) = 6, gcd(100,80) = 20, gcd(7,13) = 1. The correct output matches option A.
🧠 Conceptual
intermediate1:30remaining
Why Use Number Theory in DSA?
Why do number theory concepts like prime numbers and modular arithmetic often appear in data structure and algorithm problems?
Attempts:
2 left
💡 Hint
Think about common problem themes like hashing and cycles.
✗ Incorrect
Number theory concepts like primes and modular arithmetic help solve problems involving divisibility, cycles, and hashing efficiently, which are common in DSA.
🔧 Debug
advanced2:00remaining
Find the Error in Modular Exponentiation Code
What error does the following C code produce when computing (base^exp) % mod?
DSA C
#include <stdio.h> int mod_exp(int base, int exp, int mod) { int result = 1; for (int i = 0; i < exp; i++) { result = (result * base) % mod; } return result; } int main() { printf("%d\n", mod_exp(2, 10, 1000)); return 0; }
Attempts:
2 left
💡 Hint
Check the output of 2^10 mod 1000 carefully.
✗ Incorrect
2^10 = 1024, 1024 % 1000 = 24, so output is 24. A correctly identifies no error and the correct output. Option A is wrong output.
❓ Predict Output
advanced2:00remaining
Output of Sieve of Eratosthenes Implementation
What is the output of the following C code that prints prime numbers up to 10?
DSA C
#include <stdio.h> #include <stdbool.h> int main() { int n = 10; bool prime[11]; for (int i = 0; i <= n; i++) prime[i] = true; prime[0] = prime[1] = false; for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) { prime[j] = false; } } } for (int i = 2; i <= n; i++) { if (prime[i]) printf("%d ", i); } return 0; }
Attempts:
2 left
💡 Hint
Recall prime numbers up to 10.
✗ Incorrect
The sieve marks non-primes false. Primes up to 10 are 2, 3, 5, 7. Options A, C, D include non-primes.
🚀 Application
expert2:30remaining
Number Theory in Cycle Detection in Graphs
How can number theory concepts help in detecting cycles in graphs using algorithms like Floyd's Tortoise and Hare?
Attempts:
2 left
💡 Hint
Think about how repeated states or positions are detected in cycle detection.
✗ Incorrect
Floyd's algorithm uses two pointers moving at different speeds. Modular arithmetic helps detect repeated states efficiently by handling indices and positions in cycles.
