0
0
Goprogramming~20 mins

Practical use cases 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 code using goroutines?

Consider the following Go program that launches goroutines to print numbers. What will it print?

Go
package main

import (
	"fmt"
	"time"
)

func main() {
	for i := 0; i < 3; i++ {
		go func(i int) {
			fmt.Print(i, " ")
		}(i)
	}
	time.Sleep(100 * time.Millisecond)
}
A3 3 3
BCompilation error
C0 0 0
D0 1 2
Attempts:
2 left
💡 Hint

Think about when the goroutines capture the variable i.

Predict Output
intermediate
2:00remaining
What does this Go code print when using channels?

Examine this Go program that uses channels to communicate between goroutines. What is printed?

Go
package main

import "fmt"

func main() {
	ch := make(chan int, 2)
	ch <- 1
	ch <- 2
	fmt.Println(<-ch)
	fmt.Println(<-ch)
}
A2\n1
B1\n2
CCompilation error: channel buffer size mismatch
DDeadlock error at runtime
Attempts:
2 left
💡 Hint

Think about how buffered channels work and the order of sends and receives.

🔧 Debug
advanced
2:00remaining
Why does this Go code cause a deadlock?

Look at this Go program. Why does it cause a deadlock at runtime?

Go
package main

func main() {
	ch := make(chan int)
	ch <- 5
	<-ch
}
ABecause the channel is unbuffered and no goroutine is receiving when sending
BBecause the channel buffer is full
CBecause the receive happens before the send
DBecause the channel is closed before sending
Attempts:
2 left
💡 Hint

Think about how unbuffered channels block on send and receive.

Predict Output
advanced
2:00remaining
What is the output of this Go code using select with multiple channels?

Consider this Go program using select to receive from two channels. What will it print?

Go
package main

import (
	"fmt"
	"time"
)

func main() {
	ch1 := make(chan string)
	ch2 := make(chan string)

	go func() {
		time.Sleep(50 * time.Millisecond)
		ch1 <- "first"
	}()

	go func() {
		time.Sleep(10 * time.Millisecond)
		ch2 <- "second"
	}()

	select {
	case msg1 := <-ch1:
		fmt.Println(msg1)
	case msg2 := <-ch2:
		fmt.Println(msg2)
	}
}
ACompilation error: select cases invalid
Bfirst
Csecond
DDeadlock error at runtime
Attempts:
2 left
💡 Hint

Which channel sends first? select picks a ready channel.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of using Go's context package in concurrent programs?

Why do Go programmers use the context package when working with concurrent operations?

ATo manage cancellation, deadlines, and passing request-scoped values across goroutines
BTo improve the speed of goroutines by parallelizing CPU instructions
CTo automatically recover from panics in goroutines without crashing the program
DTo serialize data structures for network communication
Attempts:
2 left
💡 Hint

Think about how to stop or control multiple goroutines working on the same task.