Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a boolean slice to mark primes up to n.
DSA Go
primes := make([]bool, [1]+1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using size n misses the last number n.
Using size n-1 causes index out of range errors.
✗ Incorrect
We need a slice of size n+1 to include index n for marking primes.
2fill in blank
mediumComplete the code to initialize all numbers as prime (true) except 0 and 1.
DSA Go
for i := 2; i <= [1]; i++ { primes[i] = true }
Attempts:
3 left
💡 Hint
Common Mistakes
Looping only to n-1 misses the number n.
Starting from 0 or 1 incorrectly marks them as prime.
✗ Incorrect
We mark all numbers from 2 up to n as true initially.
3fill in blank
hardFix the error in the inner loop to mark multiples as not prime.
DSA Go
for j := i * i; j <= n; j += [1] { primes[j] = false }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i+1 skips some multiples.
Using 2 marks wrong multiples.
✗ Incorrect
We increment by i to mark all multiples of i as not prime.
4fill in blank
hardFill both blanks to complete the outer loop and print primes.
DSA Go
for i := 2; i <= [1]; i++ { if primes[i] == [2] { fmt.Print(i, " ") } }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for false instead of true.
Looping only to n-1 misses last prime.
✗ Incorrect
Loop up to n and print numbers marked true as primes.
5fill in blank
hardFill all three blanks to complete the Sieve of Eratosthenes function.
DSA Go
func sieve(n int) {
primes := make([]bool, [1]+1)
for i := 2; i <= n; i++ {
primes[i] = [2]
}
for i := 2; i*i <= n; i++ {
if primes[i] {
for j := i * i; j <= n; j += [3] {
primes[j] = false
}
}
}
for i := 2; i <= n; i++ {
if primes[i] {
fmt.Print(i, " ")
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Wrong slice size causes index errors.
Marking primes false initially causes no output.
Incrementing inner loop by wrong value misses multiples.
✗ Incorrect
Create slice size n+1, mark true initially, increment by i to mark multiples.
