Bird
Raised Fist0
Goprogramming~20 mins

Concurrent execution model in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Go Concurrency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Go program with goroutines?

Consider the following Go code that launches two goroutines. What will be printed?

Go
package main

import (
	"fmt"
	"time"
)

func main() {
	go fmt.Println("Hello from goroutine 1")
	go fmt.Println("Hello from goroutine 2")
	time.Sleep(100 * time.Millisecond)
	fmt.Println("Main function finished")
}
AHello from goroutine 1\nMain function finished\nHello from goroutine 2
BMain function finished\nHello from goroutine 1\nHello from goroutine 2
CMain function finished
DHello from goroutine 1\nHello from goroutine 2\nMain function finished
Attempts:
2 left
💡 Hint

Remember that goroutines run concurrently and time.Sleep allows them time to execute before the main function exits.

🧠 Conceptual
intermediate
1:30remaining
What happens when a Go channel is closed?

In Go, what is the behavior when you read from a closed channel?

AReading from a closed channel blocks forever.
BReading from a closed channel causes a runtime panic.
CReading from a closed channel returns the zero value immediately without blocking.
DReading from a closed channel returns the last value sent before closing.
Attempts:
2 left
💡 Hint

Think about how channels signal no more values will be sent.

🔧 Debug
advanced
2:00remaining
Identify the cause of deadlock in this Go program

Examine the following Go code. What causes the deadlock?

Go
package main

func main() {
	ch := make(chan int)
	ch <- 1
	<-ch
}
AThe program deadlocks because the channel is closed before receiving.
BThe program deadlocks because the send operation blocks with no receiver ready.
CThe program deadlocks because the receive operation blocks with no sender ready.
DThe program does not deadlock and prints 1.
Attempts:
2 left
💡 Hint

Consider how unbuffered channels block on send and receive.

📝 Syntax
advanced
1:30remaining
Which option correctly declares and starts a goroutine?

Which of the following Go code snippets correctly declares and starts a goroutine that prints "Hi"?

Ago func() { fmt.Println("Hi") }()
Bfunc go() { fmt.Println("Hi") }()
Cgo fmt.Println("Hi")()
Dgo func { fmt.Println("Hi") }()
Attempts:
2 left
💡 Hint

Remember the syntax for anonymous functions and starting goroutines.

🚀 Application
expert
2:30remaining
How many items are received from this buffered channel?

Given the following Go code, how many values will be received and printed?

Go
package main

import "fmt"

func main() {
	ch := make(chan int, 3)
	ch <- 10
	ch <- 20
	close(ch)

	count := 0
	for v := range ch {
		fmt.Println(v)
		count++
	}
	fmt.Println("Total received:", count)
}
A2
B0
C3
D1
Attempts:
2 left
💡 Hint

Think about how many values were sent before closing the channel.

Practice

(1/5)
1. What is a goroutine in Go's concurrent execution model?
easy
A. A lightweight thread managed by the Go runtime
B. A type of variable used for synchronization
C. A special function that runs only once
D. A package for handling errors

Solution

  1. Step 1: Understand goroutine concept

    A goroutine is a function or method that runs concurrently with other goroutines.
  2. Step 2: Identify goroutine characteristics

    It is lightweight and managed by Go's runtime, unlike OS threads which are heavier.
  3. Final Answer:

    A lightweight thread managed by the Go runtime -> Option A
  4. Quick Check:

    Goroutine = lightweight thread [OK]
Hint: Goroutine = lightweight thread in Go [OK]
Common Mistakes:
  • Confusing goroutine with OS thread
  • Thinking goroutine is a variable type
  • Assuming goroutine runs only once
2. Which of the following is the correct syntax to start a goroutine that calls function doWork()?
easy
A. go doWork()
B. goroutine doWork()
C. start doWork()
D. run doWork()

Solution

  1. Step 1: Recall goroutine syntax

    In Go, to start a goroutine, prefix the function call with the keyword go.
  2. Step 2: Match syntax with options

    Only go doWork() uses the correct keyword and syntax.
  3. Final Answer:

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

    Start goroutine with 'go' keyword [OK]
Hint: Use 'go' before function call to start goroutine [OK]
Common Mistakes:
  • Using 'goroutine' keyword instead of 'go'
  • Using 'start' or 'run' which are invalid
  • Omitting the 'go' keyword
3. What will be the output of the following Go program?
package main
import (
  "fmt"
  "time"
)
func main() {
  go fmt.Println("Hello from goroutine")
  fmt.Println("Hello from main")
  time.Sleep(100 * time.Millisecond)
}
medium
A. No output
B. Hello from goroutine\nHello from main
C. Hello from main
D. Hello from main\nHello from goroutine

Solution

  1. Step 1: Understand goroutine and main execution

    The goroutine prints "Hello from goroutine" concurrently, while main prints "Hello from main" immediately.
  2. Step 2: Analyze timing with Sleep

    The time.Sleep allows the goroutine time to run before the program exits, so both lines print.
  3. Final Answer:

    Hello from main\nHello from goroutine -> Option D
  4. Quick Check:

    Sleep allows goroutine output after main [OK]
Hint: Sleep lets goroutine finish before program ends [OK]
Common Mistakes:
  • Ignoring need for sleep to let goroutine run
  • Assuming goroutine output always prints first
  • Expecting no output due to race condition
4. Identify the error in this Go code snippet that tries to run a goroutine:
package main
import "fmt"
func main() {
  go func() {
    fmt.Println("Running")
  }()
}
medium
A. Missing import of time package
B. Program exits before goroutine runs
C. Syntax error in anonymous function
D. Cannot use go with anonymous functions

Solution

  1. Step 1: Check syntax correctness

    The anonymous function syntax and goroutine start are correct; no syntax errors.
  2. Step 2: Analyze program flow

    The main function exits immediately after starting the goroutine, so "Running" may never print.
  3. Final Answer:

    Program exits before goroutine runs -> Option B
  4. Quick Check:

    Program ends before goroutine runs [OK]
Hint: Add sleep or sync to wait for goroutine [OK]
Common Mistakes:
  • Thinking syntax is wrong for anonymous goroutine
  • Forgetting program ends immediately
  • Assuming goroutine blocks main automatically
5. You want to run 3 goroutines that each print their number (1, 2, 3). Which code correctly prints all numbers reliably before the program exits?
package main
import (
  "fmt"
  "sync"
)
func main() {
  var wg sync.WaitGroup
  for i := 1; i <= 3; i++ {
    wg.Add(1)
    go func(n int) {
      defer wg.Done()
      fmt.Println(n)
    }(i)
  }
  // What should be here?
}
hard
A. wg.Done()
B. time.Sleep(1 * time.Second)
C. wg.Wait()
D. close(wg)

Solution

  1. Step 1: Understand WaitGroup usage

    WaitGroup tracks goroutines; wg.Add(1) before start, wg.Done() after done.
  2. Step 2: Ensure main waits for all goroutines

    Calling wg.Wait() blocks main until all goroutines call Done().
  3. Final Answer:

    wg.Wait() -> Option C
  4. Quick Check:

    WaitGroup.Wait() waits for goroutines [OK]
Hint: Use wg.Wait() to wait all goroutines finish [OK]
Common Mistakes:
  • Using Sleep instead of WaitGroup for sync
  • Trying to close WaitGroup (not allowed)
  • Calling Done() in main instead of goroutine
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