KatInterview

Build a Bounded Permit Task Execution Engine

Data Structure DesignModel answer
MediumModel answerData Structure Designconcurrencysynchronizationdesign-ds

Last updated

Implement a concurrency manager that controls variable permit usage per task, supports blocking and asynchronous execution, and guarantees safe permit releases.

Design and implement a thread-safe task management component, TaskExecutor, that constrains concurrent processing using variable resource tokens (permits).

Requirements

  1. Permit Pool Initialization:
    • initialize(int maxPermits) sets up the system with a total capacity of concurrent permits.
    • Re-initialization should be rejected if active permits are currently checked out.
  2. Resource Allocation:
    • acquire(int permits) blocks the calling thread without busy-waiting until the specified number of permits are available, then reserves them.
    • release(int permits) returns the permits back to the execution engine and wakes up waiting threads.
    • Requests exceeding maxPermits must fail immediately to prevent unconditional deadlocks.
  3. Polymorphic Task Contract (ExecutableTask):
    • getRequiredPermits(): Returns the positive integer count of permits needed.
    • isBlocking(): Returns true if execute(...) must block until completion and return the direct result, or false if it should execute asynchronously in the background.
    • run(T input): Performs the domain logic on the given input.
  4. Execution Engine (execute):
    • execute(ExecutableTask task, T input) acquires the needed permits before starting the task (providing natural backpressure).
    • If the task is blocking, it runs in the caller's thread, releases its permits upon completion (even if an exception occurs), and returns the result.
    • If the task is non-blocking (asynchronous), permit ownership is transferred to a background worker thread. The caller receives a handle (such as a Future or equivalent deferred handle) immediately while the background thread releases permits upon task completion or failure.
  5. Robustness & Extensibility:
    • Permit allocation and release must be exception-safe (no resource leaks).
    • The engine must strictly depend on the ExecutableTask interface so that adding new task types does not require modifying TaskExecutor.

Provide an object-oriented implementation in Java or Python detailing the core interfaces and the TaskExecutor implementation.

Hints

Hint 1

Use explicit locks and condition variables (or standard synchronization primitives) to avoid spin-locking when waiting for available permits.

Hint 2

Consider thread safety during permit handoff for asynchronous tasks: if background thread creation fails after permits are acquired, make sure permits are released immediately in the submission thread.

Hint 3

When broadcasting availability during `release()`, ensure all waiting threads are notified (`notifyAll` / `notify_all`), because different callers may be waiting for different permit quantities.