Challenge - 5 Problems
Concurrency Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of concurrent goroutines
What is the output of this Go program that launches two goroutines printing numbers?
Go
package main import ( "fmt" "time" ) func main() { go func() { for i := 1; i <= 3; i++ { fmt.Print(i) time.Sleep(10 * time.Millisecond) } }() go func() { for i := 4; i <= 6; i++ { fmt.Print(i) time.Sleep(10 * time.Millisecond) } }() time.Sleep(50 * time.Millisecond) }
Attempts:
2 left
💡 Hint
Think about how goroutines run independently and may print in any order.
✗ Incorrect
Goroutines run concurrently, so their print statements can interleave in any order. The program prints digits 1 to 6 but the order is not guaranteed.
🧠 Conceptual
intermediate1:30remaining
Why use concurrency in Go?
Which of the following best explains why concurrency is needed in Go programs?
Attempts:
2 left
💡 Hint
Think about how computers can do many things at once.
✗ Incorrect
Concurrency lets Go programs run multiple tasks simultaneously, making better use of CPU and improving program responsiveness.
🔧 Debug
advanced2:30remaining
Identify the concurrency issue
What error or problem will this Go code cause when run?
Go
package main import ( "fmt" ) func main() { for i := 0; i < 3; i++ { go func(i int) { fmt.Println(i) }(i) } // Wait for goroutines var input string fmt.Scanln(&input) }
Attempts:
2 left
💡 Hint
Think about how the loop variable is used inside the goroutine.
✗ Incorrect
By passing the loop variable i as a parameter to the goroutine function, each goroutine gets its own copy, so it prints 0, 1, 2 as expected.
📝 Syntax
advanced1:30remaining
Identify the syntax error in concurrency code
Which option contains a syntax error in Go concurrency code?
Attempts:
2 left
💡 Hint
Check if the anonymous function is called immediately.
✗ Incorrect
Option A misses the () after the anonymous function, so the function is not called and causes a syntax error.
🚀 Application
expert3:00remaining
How many goroutines run concurrently?
Given this Go program, how many goroutines run concurrently at peak?
Go
package main import ( "fmt" "time" ) func worker(id int) { fmt.Printf("Worker %d starting\n", id) time.Sleep(50 * time.Millisecond) fmt.Printf("Worker %d done\n", id) } func main() { for i := 1; i <= 5; i++ { go worker(i) } time.Sleep(100 * time.Millisecond) }
Attempts:
2 left
💡 Hint
Count how many goroutines are started and how long they run.
✗ Incorrect
The program starts 5 goroutines almost at the same time, so all 5 run concurrently during the sleep period.