Design an AsyncLimit concurrency limiter that queues tasks over the limit in FIFO order
Company: Agoda
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a concurrency control utility `AsyncLimit` that limits the number of tasks running in parallel to a fixed `limit`. When the limiter is at capacity, additional tasks must wait in a **FIFO queue** and start as soon as a running task finishes.
Because asynchronous behavior is simulated synchronously, the class exposes methods to submit a task (identified by a unique `taskId`), complete a running task, and inspect the current running set and pending queue.
When `submit(taskId)` is called: if fewer than `limit` tasks are currently running, the task starts immediately and the method returns `True`. Otherwise the task is enqueued and the method returns `False`. When `complete(taskId)` is called, that task is removed from the running set and, if any tasks are queued, the oldest queued task is promoted to running.
Implement the following methods:
- `AsyncLimit(limit: int)`: create a limiter that allows at most `limit` tasks to run at once.
- `submit(taskId: int) -> bool`: attempt to start a task. Returns `True` if it started immediately, or `False` if it was queued because the limiter is at capacity.
- `complete(taskId: int)`: mark a currently running task as finished. If any tasks are queued, the oldest queued task is promoted to running.
- `running_tasks() -> List[int]`: return the ids of all currently running tasks, sorted in ascending order.
- `pending_count() -> int`: return the number of tasks currently waiting in the pending queue.
Example 1:
```text
Input:
["AsyncLimit", "submit", "submit", "submit", "running_tasks", "pending_count", "complete", "running_tasks", "pending_count"]
[[2], [1], [2], [3], [], [], [1], [], []]
Output: [null, true, true, false, [1, 2], 1, null, [2, 3], 0]
limiter = AsyncLimit(2) # limit = 2
limiter.submit(1) # True (slot free, running: {1})
limiter.submit(2) # True (slot free, running: {1, 2})
limiter.submit(3) # False (at capacity, queued: [3])
limiter.running_tasks() # [1, 2]
limiter.pending_count() # 1
limiter.complete(1) # task 3 promoted, running: {2, 3}
limiter.running_tasks() # [2, 3]
limiter.pending_count() # 0
```
Example 2:
```text
Input:
["AsyncLimit", "submit", "submit", "submit", "submit", "pending_count", "complete", "running_tasks", "pending_count", "complete", "running_tasks"]
[[1], [10], [20], [30], [40], [], [10], [], [], [20], []]
Output: [null, true, false, false, false, 3, null, [20], 2, null, [30]]
limiter = AsyncLimit(1) # limit = 1
limiter.submit(10) # True (running: {10})
limiter.submit(20) # False (queued: [20])
limiter.submit(30) # False (queued: [20, 30])
limiter.submit(40) # False (queued: [20, 30, 40])
limiter.pending_count() # 3
limiter.complete(10) # 20 promoted, running: {20}, queued: [30, 40]
limiter.running_tasks() # [20]
limiter.pending_count() # 2
limiter.complete(20) # 30 promoted, running: {30}, queued: [40]
limiter.running_tasks() # [30]
```
```hint Match each group to a container
List the operations the running group and the waiting group each need (add, remove by id, remove the oldest, count) and pick a container that does each one in constant time. Watch the cost of removing the first element of a plain Python list.
```
### Constraints & Assumptions
- `1 <= limit <= 10⁴`
- `1 <= taskId <= 10⁹`
- All submitted `taskId`s within a single `AsyncLimit` instance are unique.
- `complete` is only called with a `taskId` currently in the running set.
- At most `10⁵` operations will be performed.
### What a Strong Answer Covers
- A choice of containers for running and pending tasks that keeps `submit`, `complete` and `pending_count` at constant time.
- Promotion on `complete` that preserves FIFO order and never lets the running count exceed `limit`.
- Deterministic sorted output from `running_tasks`, and what producing it costs.
- Time and space complexity for every method.
- Behavior at the edges: `limit = 1`, `complete` with an empty queue, and a burst of submissions that all queue.
### Follow-up Questions
- How would you add `cancel(taskId)` for a task that is still pending, without making the other operations slower?
- How would you build the real asynchronous version with Python's asyncio or JavaScript promises, where `submit` takes a coroutine or function and returns its result?
- What breaks if several threads call `submit` and `complete` at the same time, and how would you fix it?
- How would you support task priorities, so a high-priority task starts ahead of older low-priority ones?
Overview: Design an AsyncLimit class that caps how many tasks run at once and queues extra tasks in FIFO order, with methods to submit, complete and inspect tasks. It tests container choice, keeping the concurrency limit invariant across promotions, and stating the cost of each operation.