0
0
Goprogramming~20 mins

Common concurrency patterns in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Concurrency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple goroutine with channel communication

What is the output of this Go program?

Go
package main

import (
	"fmt"
)

func main() {
	ch := make(chan string)
	go func() {
		ch <- "hello"
	}()
	msg := <-ch
	fmt.Println(msg)
}
Ahello
BCompilation error
CDeadlock error
DEmpty output
Attempts:
2 left
💡 Hint

Think about how channels synchronize goroutines.

Predict Output
intermediate
2:00remaining
Output of select with multiple channel cases

What will this Go program print?

Go
package main

import (
	"fmt"
	"time"
)

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

	go func() {
		time.Sleep(100 * time.Millisecond)
		ch1 <- "one"
	}()

	go func() {
		ch2 <- "two"
	}()

	select {
	case msg1 := <-ch1:
		fmt.Println(msg1)
	case msg2 := <-ch2:
		fmt.Println(msg2)
	}
}
Atwo
BDeadlock error
CCompilation error
Done
Attempts:
2 left
💡 Hint

Which channel sends first without delay?

Predict Output
advanced
2:00remaining
Output of a fan-in pattern using channels

What is the output of this Go program?

Go
package main

import (
	"fmt"
)

func worker(id int, ch chan<- string) {
	ch <- fmt.Sprintf("worker %d done", id)
}

func main() {
	ch := make(chan string)
	for i := 1; i <= 3; i++ {
		go worker(i, ch)
	}
	for i := 1; i <= 3; i++ {
		msg := <-ch
		fmt.Println(msg)
	}
}
A
worker 3 done
worker 2 done
worker 1 done
BDeadlock error
CCompilation error
D
worker 1 done
worker 2 done
worker 3 done
Attempts:
2 left
💡 Hint

Order of output may vary but all workers send a message.

Predict Output
advanced
2:00remaining
Output of a pipeline pattern with channel chaining

What does this Go program print?

Go
package main

import (
	"fmt"
)

func gen(nums ...int) <-chan int {
	out := make(chan int)
	go func() {
		for _, n := range nums {
			out <- n
		}
		close(out)
	}()
	return out
}

func sq(in <-chan int) <-chan int {
	out := make(chan int)
	go func() {
		for n := range in {
			out <- n * n
		}
		close(out)
	}()
	return out
}

func main() {
	c := gen(2, 3, 4)
	out := sq(c)
	for n := range out {
		fmt.Println(n)
	}
}
ADeadlock error
B
4
9
16
C
2
3
4
DCompilation error
Attempts:
2 left
💡 Hint

Think about how data flows through the pipeline.

Predict Output
expert
2:00remaining
Output of a race condition with shared variable

What is the output of this Go program?

Go
package main

import (
	"fmt"
	"sync"
)

func main() {
	var counter int
	var wg sync.WaitGroup
	wg.Add(2)
	go func() {
		for i := 0; i < 1000; i++ {
			counter++
		}
		wg.Done()
	}()
	go func() {
		for i := 0; i < 1000; i++ {
			counter++
		}
		wg.Done()
	}()
	wg.Wait()
	fmt.Println(counter)
}
A2000
BCompilation error
CA number less than 2000
DDeadlock error
Attempts:
2 left
💡 Hint

Think about concurrent access to the variable without synchronization.