Bird
Raised Fist0
Goprogramming~20 mins

Why concurrency is needed in Go - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Concurrency Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
}
A123456
BAny interleaved combination of digits 1 to 6
C456123
DProgram does not print anything
Attempts:
2 left
💡 Hint
Think about how goroutines run independently and may print in any order.
🧠 Conceptual
intermediate
1:30remaining
Why use concurrency in Go?
Which of the following best explains why concurrency is needed in Go programs?
ATo prevent any code from running in parallel
BTo make programs run slower and use more memory
CTo avoid using functions and loops
DTo allow multiple tasks to run at the same time improving efficiency and responsiveness
Attempts:
2 left
💡 Hint
Think about how computers can do many things at once.
🔧 Debug
advanced
2: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)
}
APrints 0, 1, 2 as expected
BCompilation error due to missing import
CPrints 3, 3, 3 because of variable capture in goroutine
DDeadlock error at runtime
Attempts:
2 left
💡 Hint
Think about how the loop variable is used inside the goroutine.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in concurrency code
Which option contains a syntax error in Go concurrency code?
Ago func() { fmt.Println("Hello") }
B)(} )"olleH"(nltnirP.tmf { )(cnuf og
Cgo func() { fmt.Println("Hello") }()
Do func() { fmt.Println("Hello") }()
Attempts:
2 left
💡 Hint
Check if the anonymous function is called immediately.
🚀 Application
expert
3: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)
}
ANo goroutines run concurrently
B1 goroutine at a time
C5 goroutines run concurrently
D10 goroutines run concurrently
Attempts:
2 left
💡 Hint
Count how many goroutines are started and how long they run.

Practice

(1/5)
1. Why do we need concurrency in Go programs?
easy
A. To simplify the code by removing all loops and conditions.
B. To allow multiple tasks to run at the same time, improving speed and responsiveness.
C. To make the program use less memory by running tasks one after another.
D. To avoid using functions and write everything in the main block.

Solution

  1. Step 1: Understand concurrency purpose

    Concurrency allows a program to handle multiple tasks at once instead of waiting for one to finish before starting another.
  2. Step 2: Relate concurrency to Go

    Go uses goroutines to run tasks concurrently, which helps programs be faster and more responsive.
  3. Final Answer:

    To allow multiple tasks to run at the same time, improving speed and responsiveness. -> Option B
  4. Quick Check:

    Concurrency = multiple tasks at once [OK]
Hint: Concurrency means doing many things at once [OK]
Common Mistakes:
  • Thinking concurrency reduces memory by running tasks sequentially
  • Confusing concurrency with simpler code structure
  • Believing concurrency removes the need for functions
2. Which of the following is the correct way to start a goroutine in Go?
easy
A. go myFunction()
B. start myFunction()
C. run myFunction()
D. goroutine myFunction()

Solution

  1. Step 1: Recall Go syntax for goroutines

    In Go, the keyword go is used before a function call to start it as a goroutine.
  2. Step 2: Check each option

    Only go myFunction() uses the correct go keyword syntax; others are invalid in Go.
  3. Final Answer:

    go myFunction() -> Option A
  4. Quick Check:

    Start goroutine with 'go' keyword [OK]
Hint: Use 'go' before function to start goroutine [OK]
Common Mistakes:
  • Using 'start' or 'run' instead of 'go'
  • Forgetting the 'go' keyword before function call
  • Trying to declare goroutine with 'goroutine' keyword
3. What will be the output of this Go program?
package main
import (
  "fmt"
  "time"
)
func sayHello() {
  fmt.Println("Hello")
}
func main() {
  go sayHello()
  time.Sleep(1 * time.Second)
  fmt.Println("World")
}
medium
A. World\nHello
B. Hello
C. Hello\nWorld
D. World

Solution

  1. Step 1: Understand goroutine and sleep

    The function sayHello runs as a goroutine, so it runs concurrently with main. The main function sleeps 1 second to allow sayHello to print.
  2. Step 2: Determine print order

    Because of the sleep, "Hello" prints first from the goroutine, then "World" prints after sleep ends.
  3. Final Answer:

    Hello World -> Option C
  4. Quick Check:

    Goroutine prints 'Hello' before main prints 'World' [OK]
Hint: Sleep lets goroutine finish before main prints [OK]
Common Mistakes:
  • Ignoring time.Sleep and missing 'Hello' output
  • Assuming 'World' prints before 'Hello'
  • Thinking goroutine blocks main function
4. Identify the problem in this Go code snippet:
package main
import "fmt"
func main() {
  go func() {
    fmt.Println("Running")
  }()
  fmt.Println("Done")
}
medium
A. Goroutines cannot be used inside main function.
B. The anonymous function syntax is incorrect.
C. The fmt package is not imported properly.
D. The program may exit before the goroutine runs, so "Running" might not print.

Solution

  1. Step 1: Analyze goroutine execution timing

    The goroutine runs concurrently, but main may finish and exit before the goroutine prints "Running".
  2. Step 2: Check code correctness

    The anonymous function syntax and imports are correct; goroutines can run inside main.
  3. Final Answer:

    The program may exit before the goroutine runs, so "Running" might not print. -> Option D
  4. Quick Check:

    Program exit can stop goroutine early [OK]
Hint: Main may exit before goroutine finishes [OK]
Common Mistakes:
  • Thinking anonymous function syntax is wrong
  • Believing goroutines can't run in main
  • Ignoring program exit timing
5. You want to download 3 files concurrently in Go and print "All done" only after all downloads finish. Which approach correctly uses concurrency to achieve this?
hard
A. Start 3 goroutines for downloads and use a sync.WaitGroup to wait for all to finish before printing "All done".
B. Call the download functions one after another without goroutines, then print "All done".
C. Start 3 goroutines but print "All done" immediately after starting them without waiting.
D. Use a for loop to start downloads but do not synchronize; rely on time.Sleep to wait.

Solution

  1. Step 1: Understand concurrency with synchronization

    Starting goroutines runs downloads concurrently, but we must wait for all to finish before printing "All done".
  2. Step 2: Use sync.WaitGroup for waiting

    sync.WaitGroup lets main wait until all goroutines complete, ensuring correct order.
  3. Final Answer:

    Start 3 goroutines for downloads and use a sync.WaitGroup to wait for all to finish before printing "All done". -> Option A
  4. Quick Check:

    WaitGroup synchronizes goroutines before final print [OK]
Hint: Use WaitGroup to wait for all goroutines [OK]
Common Mistakes:
  • Not waiting for goroutines before printing
  • Using sleep instead of proper synchronization
  • Running downloads sequentially without concurrency
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard