0
0
Goprogramming~20 mins

Concurrent execution model in Go - Practice Problems & Coding Challenges

Choose your learning style9 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.