Katkat

Implement a Thread-Safe Per-Key Ordered Executor

MicrosoftSoftware Engineer · Onsite

Last updated

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

  • 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?

Hints

Hint 1

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.

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?