Implement an in-process component constructed with `maxConcurrent` and an existing thread-pool executor. It exposes `submit(key, task)` and must preserve task order within each key while allowing independent keys to execute concurrently.
### Constraints & Assumptions
- Multiple threads may call `submit` concurrently. Define the linearization point that establishes order for concurrent submissions sharing a key.
- Tasks with one key must execute in submission order and must never overlap.
- Tasks with different keys may run concurrently, with at most `maxConcurrent` tasks running globally.
- `submit` returns after enqueueing/scheduling; it does not wait for the task to finish.
- Do not implement the underlying thread pool. For the core algorithm, assume it accepts scheduled work while this component is open and that tasks eventually return or throw.
- A long task for one key must not hold a lock or occupy extra waiting workers that unnecessarily block other keys when capacity is available.
### Clarifying Questions to Ask
- Should queue capacity be bounded, and how should overload be reported?
- What should task exceptions do to later tasks for the same key?
- Is shutdown or executor rejection part of the required interface?
```hint Schedule ready keys, not blocked tasks
Only the next eligible task for a key should consume a global execution slot. Later tasks for that key can remain in its own queue.
```
### What a Strong Answer Covers
- A per-key FIFO queue, an eligibility state, and a global concurrency count.
- Thread-safe submission and completion transitions with a clear ordering definition.
- Dispatch without scanning all other key queues or executing user work under the scheduler lock.
- Slot release and continued progress after task exceptions.
- Cleanup of idle keys, fairness considerations, and explicit lifecycle assumptions.
### Follow-up Questions
- How would simultaneous submissions for the same key be ordered?
- How would you add bounded queues and shutdown without losing accepted tasks?
Overview: Implement per-key FIFO task execution with thread-safe submission, bounded global concurrency, ready-key scheduling, and exception-safe slot release.
Implement an in-process component constructed with maxConcurrent and an existing thread-pool executor. It exposes submit(key, task) and must preserve task order within each key while allowing independent keys to execute concurrently.
Constraints & Assumptions
Multiple threads may call
submit
concurrently. Define the linearization point that establishes order for concurrent submissions sharing a key.
Tasks with one key must execute in submission order and must never overlap.
Tasks with different keys may run concurrently, with at most
maxConcurrent
tasks running globally.
submit
returns after enqueueing/scheduling; it does not wait for the task to finish.
Do not implement the underlying thread pool. For the core algorithm, assume it accepts scheduled work while this component is open and that tasks eventually return or throw.
A long task for one key must not hold a lock or occupy extra waiting workers that unnecessarily block other keys when capacity is available.
Clarifying Questions to Ask Guidance
Should queue capacity be bounded, and how should overload be reported?
What should task exceptions do to later tasks for the same key?
Is shutdown or executor rejection part of the required interface?
What a Strong Answer Covers Guidance
A per-key FIFO queue, an eligibility state, and a global concurrency count.
Thread-safe submission and completion transitions with a clear ordering definition.
Dispatch without scanning all other key queues or executing user work under the scheduler lock.
Slot release and continued progress after task exceptions.
Cleanup of idle keys, fairness considerations, and explicit lifecycle assumptions.
Follow-up Questions Guidance
How would simultaneous submissions for the same key be ordered?
How would you add bounded queues and shutdown without losing accepted tasks?