codejump
HardPro challengePythonJavaScriptTypeScript

asyncio.Semaphore — Rate-Limit Concurrent Tasks

PythonAsyncConcurrency

Implement solve(items, limit) that processes a list of integers concurrently but allows at most limit tasks running at the same time.

Each "task" doubles its input value. Return the results in the same order as input.

Examples

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

Constraints

  • Use asyncio.Semaphore(limit) to cap concurrency.
  • Results must preserve input order.

In JavaScript / TypeScript — Implement the small Semaphore class of the starter (acquire() waits for a free slot, release() hands it to the next waiter), then process every item through it with Promise.all.

Sample tests

Test #1Single item
Input: [[10],1]
Output: [20]
Test #2Five tasks, limit 2
Input: [[1,2,3,4,5],2]
Output: [2,4,6,8,10]
Test #3Limit equals count — all run at once
Input: [[1,2,3],3]
Output: [2,4,6]