
Practice 50 Go Lang Concurrency Coding Questions
Q1. Write a Go program to demonstrate the use of goroutines.
Input:
Goroutine prints "Hello from Goroutine"
Expected Output:
Hello from Goroutine
Q2. Write a Go program to execute two goroutines concurrently and wait for their completion.
Input:
Goroutine 1 prints "First"
Goroutine 2 prints "Second"
Expected Output:
First
Second
Q3. Write a Go program to demonstrate the use of channels to send and receive data between goroutines.
Input:
Channel passes "Hello from main"
Expected Output:
Hello from main
Q4. Write a Go program to demonstrate the use of `select` to handle multiple channels.
Input:
Channel 1 sends "Message 1"
Channel 2 sends "Message 2"
Expected Output:
Message 1
Message 2
Q5. Write a Go program to demonstrate the use of `sync.WaitGroup` to wait for multiple goroutines to complete.
Input:
Goroutines print "Task completed"
Expected Output:
Task completed
Task completed
Task completed
Q6. Write a Go program to create a buffered channel and send data into it.
Input:
Channel buffer size = 3
Send "Item 1", "Item 2", "Item 3" into the channel
Expected Output:
Item 1
Item 2
Item 3
Q7. Write a Go program to use `defer` in goroutines.
Input:
Defer print statement inside a goroutine
Expected Output:
Deferred message from goroutine
Q8. Write a Go program to send a struct over a channel and print its fields in a goroutine.
Input:
struct { Name: "Alice", Age: 30 }
Expected Output:
Name: Alice, Age: 30
Q9. Write a Go program to demonstrate a simple worker pool using goroutines.
Input:
Worker 1, Worker 2 processes tasks "Task 1", "Task 2"
Expected Output:
Task 1 processed
Task 2 processed
Q10. Write a Go program to send and receive a string using channels in goroutines.
Input:
Channel sends "Hello"
Expected Output:
Received: Hello
Q11. Write a Go program to use multiple goroutines and channels to sum the elements of a slice.
Input:
Slice: [1, 2, 3, 4]
Expected Output:
Sum: 10
Q12. Write a Go program to demonstrate the use of `sync.Mutex` to avoid race conditions.
Input:
Two goroutines increment the shared counter variable
Expected Output:
Counter: 2
Q13. Write a Go program to demonstrate the `sync.Once` functionality.
Input:
Function should only be called once, even if invoked multiple times.
Expected Output:
Initialized
Q14. Write a Go program to use channels to synchronize the completion of tasks between multiple goroutines.
Input:
Goroutines complete tasks: "Task A", "Task B", "Task C"
Expected Output:
Task A completed
Task B completed
Task C completed
Q15. Write a Go program to demonstrate the use of a `time.Timer` in a goroutine.
Input:
Timer expires after 2 seconds and prints "Timeout"
Expected Output:
Timeout
Q16. Write a Go program to use `sync.Cond` to synchronize goroutines.
Input:
Condition variable is used to notify goroutines to proceed
Expected Output:
Goroutine started
Q17. Write a Go program to demonstrate how goroutines can communicate using channels.
Input:
Main goroutine sends "Hello" to another goroutine via a channel
Expected Output:
Hello from the other goroutine
Q18. Write a Go program to implement a producer-consumer problem using goroutines and channels.
Input:
Producer produces "item 1", "item 2", and "item 3"
Consumer consumes the items
Expected Output:
Consumed: item 1
Consumed: item 2
Consumed: item 3
Q19. Write a Go program to demonstrate the use of `select` to handle multiple channels with different timeouts.
Input:
Channel 1 sends "Message A"
Channel 2 sends "Message B"
Timeout is set to 1 second
Expected Output:
Message A
Q20. Write a Go program to use goroutines to calculate the factorial of a number concurrently.
Input:
Calculate factorial of 5
Expected Output:
Factorial of 5: 120
Q21. Write a Go program to demonstrate the use of `time.Sleep` to introduce a delay in a goroutine.
Input:
Sleep for 3 seconds and then print "Done"
Expected Output:
Done
Q22. Write a Go program to create multiple goroutines that each print their own index in a loop.
Input:
Create 5 goroutines, each printing its index
Expected Output:
0
1
2
3
4
Q23. Write a Go program to demonstrate the use of a `time.After` channel to trigger an action after a specified delay.
Input:
Action triggered after 2 seconds
Expected Output:
Action executed after timeout
Q24. Write a Go program to implement a simple gossip protocol with goroutines.
Input:
Node 1 gossips to Node 2 and Node 3
Expected Output:
Node 1: Gossiped to Node 2
Node 1: Gossiped to Node 3
Q25. Write a Go program to create a goroutine that simulates a clock and sends the current time every second.
Input:
Current time printed every second
Expected Output:
12:00:01
12:00:02
12:00:03
Q26. Write a Go program to demonstrate the use of `sync.WaitGroup` to wait for multiple goroutines to complete.
Input:
3 goroutines perform different tasks
Expected Output:
Task 1 completed
Task 2 completed
Task 3 completed
Q27. Write a Go program to send an integer over a channel and receive it in a separate goroutine.
Input:
Send value 10 via channel
Expected Output:
Received: 10
Q28. Write a Go program to implement the producer-consumer problem with a single shared buffer using goroutines and channels.
Input:
Producer produces 3 items, consumer consumes them
Expected Output:
Consumed: Item 1
Consumed: Item 2
Consumed: Item 3
Q29. Write a Go program that launches multiple goroutines and prints a message from each.
Input:
5 goroutines print a message concurrently
Expected Output:
Message from goroutine 1
Message from goroutine 2
Message from goroutine 3
Message from goroutine 4
Message from goroutine 5
Q30. Write a Go program to use a `defer` statement in a goroutine to print a message after completion.
Input:
Print "Task started" and defer "Task finished"
Expected Output:
Task started
Task finished
Q31. Write a Go program to implement a counter that uses goroutines and channels to increment the value concurrently.
Input:
Increment counter concurrently by 1 in multiple goroutines
Expected Output:
Counter: 5
Q32. Write a Go program to create a goroutine that simulates a countdown timer.
Input:
Countdown from 5 to 1
Expected Output:
5
4
3
2
1
Q33. Write a Go program to use a channel to collect the results of multiple goroutines concurrently performing different calculations.
Input:
Goroutines calculate 10*2, 5+3, and 6/2
Expected Output:
Results: 20, 8, 3
Q34. Write a Go program to demonstrate the use of `sync.Mutex` to ensure that only one goroutine accesses a shared resource at a time.
Input:
Mutex protects the shared variable during increments
Expected Output:
Final value: 10
Q35. Write a Go program that uses a `select` statement to handle multiple channel operations.
Input:
Channels send "A", "B", and "C"
Expected Output:
A
B
C
Q36. Write a Go program to use `sync.Once` to ensure a function is executed only once.
Input:
Execute the initialization function only once
Expected Output:
Initialization complete
Q37. Write a Go program to launch multiple goroutines that each increment a shared variable.
Input:
Shared variable increments 5 times
Expected Output:
Final shared value: 5
Q38. Write a Go program that demonstrates the use of `sync/atomic` to safely increment a variable in multiple goroutines.
Input:
Use atomic.AddInt32 to increment the variable
Expected Output:
Final value: 100
Q39. Write a Go program to show the differences between buffered and unbuffered channels.
Input:
Unbuffered channel sends "Message" and receives it
Buffered channel sends "Buffered Message"
Expected Output:
Message
Buffered Message
Q40. Write a Go program to demonstrate the use of `time.Tick` to trigger periodic actions in a goroutine.
Input:
Print message every 1 second
Expected Output:
Tick
Tick
Tick
Q41. Write a Go program to simulate a simple job scheduler using goroutines.
Input:
Schedule 3 tasks: "Task 1", "Task 2", "Task 3"
Expected Output:
Task 1 executed
Task 2 executed
Task 3 executed
Q42. Write a Go program that demonstrates the concept of deadlock using goroutines.
Input:
Goroutines lock two resources and wait indefinitely
Expected Output:
Deadlock occurred
Q43. Write a Go program to implement a "ping-pong" system with two goroutines sending messages to each other.
Input:
Goroutine 1 sends "Ping", Goroutine 2 replies "Pong"
Expected Output:
Ping
Pong
Q44. Write a Go program to demonstrate the use of `time.AfterFunc` to schedule a function execution after a delay.
Input:
Print message after 2 seconds
Expected Output:
Function executed after delay
Q45. Write a Go program that shows how a goroutine waits for a signal from another goroutine before it proceeds.
Input:
Goroutine waits for "Start Signal"
Expected Output:
Proceeding after signal received
Q46. Write a Go program that uses a goroutine to process items in a queue.
Input:
Queue contains ["Item 1", "Item 2", "Item 3"]
Expected Output:
Processing: Item 1
Processing: Item 2
Processing: Item 3
Q47. Write a Go program to demonstrate using a worker pool to process multiple jobs concurrently.
Input:
Worker pool processes jobs "Job 1", "Job 2", "Job 3"
Expected Output:
Job 1 processed
Job 2 processed
Job 3 processed
Q48. Write a Go program to send a message to a goroutine and have it respond asynchronously.
Input:
Send "Hello" to the goroutine
Expected Output:
Response: Hello
Q49. Write a Go program that demonstrates the use of a `sync.Pool` to manage temporary objects.
Input:
Use sync.Pool to create and reuse an integer
Expected Output:
Reused object: 42
Q50. Write a Go program that uses `context` to manage cancellation in goroutines.
Input:
Goroutine cancels operation after 2 seconds
Expected Output:
Operation canceled