Challenge - 5 Problems
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 a Loop
What is the output of this Go code that calculates the GCD of pairs of numbers in a slice?
DSA Go
package main import "fmt" func gcd(a, b int) int { for b != 0 { a, b = b, a%b } return a } func main() { pairs := [][2]int{{12, 18}, {100, 80}, {7, 3}} for _, p := range pairs { fmt.Print(gcd(p[0], p[1]), " ") } }
Attempts:
2 left
💡 Hint
Recall that GCD is the greatest common divisor, and 7 and 3 are coprime.
✗ Incorrect
The GCD of 12 and 18 is 6, of 100 and 80 is 20, and of 7 and 3 is 1 because they share no common divisors other than 1.
🧠 Conceptual
intermediateWhy Use Prime Factorization in DSA?
Why is prime factorization important in solving some DSA problems?
Attempts:
2 left
💡 Hint
Think about how prime numbers relate to multiplication and division.
✗ Incorrect
Prime factorization breaks numbers into prime components, which helps in problems involving divisibility, LCM, GCD, and detecting patterns in numbers.
🔧 Debug
advancedIdentify the Error in Modular Exponentiation Code
What error will this Go code produce when calculating (base^exp) % mod?
DSA Go
package main import "fmt" func modExp(base, exp, mod int) int { if exp == 0 { return 1 } half := modExp(base, exp/2, mod) result := (half * half) % mod if exp%2 == 1 { result = (result * base) % mod } return result } func main() { fmt.Println(modExp(2, 10, 1000)) }
Attempts:
2 left
💡 Hint
Check the modular exponentiation logic and the expected output of 2^10 mod 1000.
✗ Incorrect
2^10 = 1024, and 1024 % 1000 = 24, so the code correctly prints 24 without errors.
❓ Predict Output
advancedOutput of Sieve of Eratosthenes Implementation
What is the output of this Go code that finds primes up to 10 using the sieve method?
DSA Go
package main import "fmt" func sieve(n int) []int { prime := make([]bool, n+1) for i := 2; i <= n; i++ { prime[i] = true } for p := 2; p*p <= n; p++ { if prime[p] { for i := p * p; i <= n; i += p { prime[i] = false } } } var primes []int for i := 2; i <= n; i++ { if prime[i] { primes = append(primes, i) } } return primes } func main() { fmt.Println(sieve(10)) }
Attempts:
2 left
💡 Hint
Remember that primes are numbers greater than 1 with no divisors other than 1 and themselves.
✗ Incorrect
The sieve correctly marks 2, 3, 5, and 7 as primes. Numbers like 4, 9 are not prime.
🧠 Conceptual
expertWhy Are Number Theory Concepts Crucial in Cryptography Algorithms?
Why do cryptography algorithms rely heavily on number theory concepts like primes and modular arithmetic?
Attempts:
2 left
💡 Hint
Think about what makes encryption keys difficult to break.
✗ Incorrect
Cryptography uses primes and modular arithmetic to create problems that are easy to compute one way but hard to reverse without keys, ensuring security.
