← All Exercises

Bounded Worker Pool with Ordered Results

intermediatego~30min

Background

A bounded pool caps memory and downstream load; carrying each job's index through the result channel lets the collector restore order without sorting.

Problem

Implement squareAll(jobs []int, workers int) ([]int, error) that squares every job using at most workers goroutines, closes all channels, and returns results in the original order. Reject workers < 1 and detect missing results instead of returning zero values.

Input

A slice of integers and a positive worker count.

Output

A slice of squared values in input order, or an error for invalid configuration or missing results.

Sample tests

Sample 1

Input: jobs = [1, 2, 3, 4, 5], workers = 2
Expected: [1, 4, 9, 16, 25]

Sample 2

Input: jobs = [], workers = 3
Expected: []

Sample 3

Input: jobs = [3, 1], workers = 0
Expected: error: workers must be at least 1

Your solution

Learning objective

Coordinate a feeder, fixed workers, and a collector with WaitGroup and channels without deadlocks.