Sobes.tech

Task: "Ping-pong with timeout" Write a function pingPong that: - Starts two goroutines: one generates "ping" messages, the other — "pong". - Goroutines should exchange messages through a shared channel strictly in turn. - The exchange continues for 3 seconds, after which the program should terminate (graceful shutdown). - Use context for cancellation via timeout. - Output each message on the screen along with the time (in milliseconds from start). Example of expected output: 0ms: ping

216

A simple live coding task (basic concurrency) Condition: Write a function fetchAll(urls []string) map[string]string that performs HTTP GET requests concurrently for each URL and returns a map where the key is the URL and the value is the response body (string). If the request fails (error, not 200), the value should be an empty string. Constraints: - Do not use external libraries (only standard). - Do not change the signature. - The program should not crash on panic in http.Get. Example input: ["[link]" "[link]" "[link]"]

203

package main import ( "fmt" ) func a(p *string) { s2 := "string2" *p = s2 } func main() { str1 := "string1" p := &str1 fmt.Println(*p) a(&p) fmt.Println(*p) }

187

What to say to the interviewer (if asked) "I would use a worker pool with a fixed number of workers, a channel for tasks, and a sync.WaitGroup. The workers will execute requests, and timeout and cancellation will be controlled through the context. I will collect the results in a slice, protected by a mutex (or through a separate channel)."} Now it's your turn Write the code in this chat. I will review and give feedback. If you don't know where to start – ask for a hint. Good luck!

177

// all elements of the slice should be unique + consist of 1000 elements in total // the program should not hang due to time.sleep func main() { checkIncrement := map[int]int32{} var data []int for i := 0; i < 1000; i++ { go func() { data = append(data, i) if _, found := checkIncrement[i]; found { fmt.Println(i) panic("duplicated increment") } checkIncrement[i] = 1 }() } time.Sleep(2 * time.Second) fmt.Println(len(data)) }

159

What is a pointer and what happens if a function argument is a pointer?

159

Fill a slice with elements without a mutex so that it contains all 1000 elements.

148