codejump
HardPro challengePythonJavaScriptTypeScript

threading.Queue — Producer / Consumer

PythonThreadingConcurrency

Implement solve(items, num_consumers) using the producer/consumer pattern:

1. A producer thread puts each item from items into a queue.Queue.
2. num_consumers consumer threads each pop items, double the value, and store results.
3. Return the results sorted ascending (order is non-deterministic with threads).

Examples

  • solve([1, 2, 3], 2) → [2, 4, 6]
  • solve([5], 1) → [10]

Constraints

  • Use queue.Queue for thread-safe communication.
  • Signal consumers with sentinel values (None) when production is done.
  • All threads must finish before returning.

In JavaScript / TypeScript — JavaScript has no threads, but the pattern is identical with async tasks: implement the starter's AsyncQueue (get() waits until an item arrives), run one producer and numConsumers consumers concurrently, and stop each consumer on a null sentinel.

Sample tests

Test #1Three items, two consumers
Input: [[1,2,3],2]
Output: [2,4,6]
Test #2Single item, single consumer
Input: [[5],1]
Output: [10]
Test #3Empty items
Input: [[],2]
Output: []