DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
MediumMachine Coding

Machine Coding: AI-IDE Task Scheduler

6 views

Problem Statement

In high-performance IDEs and AI-assisted editors, managing background tasks is critical. You need to implement a Priority Task Queue with Concurrency Control.

Your task is to create a TaskScheduler class that can handle multiple tasks with different priorities (High, Medium, Low). The scheduler should limit the number of concurrently running tasks to avoid slowing down the editor's main thread or overwhelming the AI service APIs.

Requirements

  1. Concurrency Limit: The scheduler should accept a maxConcurrency value in its constructor.
  2. Priority Levels: Tasks are added with a priority: 0 (High), 1 (Medium), and 2 (Low). High-priority tasks should always run before lower-priority ones.
  3. Task Execution: Tasks are asynchronous functions that return a Promise.
  4. Result Management: The scheduler should return a promise that resolves with the task's result when it completes.

Example


const scheduler = new TaskScheduler(2); // Max 2 tasks at a time

const task1 = () => new Promise(res => setTimeout(() => res('Task 1'), 1000));
const task2 = () => new Promise(res => setTimeout(() => res('Task 2'), 500));

scheduler.addTask(task1, 1).then(console.log); // Medium priority
scheduler.addTask(task2, 0).then(console.log); // High priority

Constraints

  • The number of active promises must never exceed maxConcurrency.
  • If a High priority task enters the queue while the scheduler is full, it must be the next task to run once a slot opens up.

Sample Test Cases

Case 1
Input
{"maxConcurrency": 2, "tasks": [{"id": "task1", "priority": 1, "delay": 100, "result": "Task 1"}, {"id": "task2", "priority": 0, "delay": 50, "result": "Task 2"}, {"id": "task3", "priority": 2, "delay": 150, "result": "Task 3"}]}
Expected Output
["Task 2", "Task 1", "Task 3"]
Case 2
Input
[
  2,
  [
    {"taskName": "Task 1", "priority": 1, "delay": 100},
    {"taskName": "Task 2", "priority": 0, "delay": 50}
  ]
]
Expected Output
["Task 2", "Task 1"]
Case 3
Input
{"maxConcurrency": 1, "tasks": [{"id": "taskA", "priority": 0, "delay": 100, "result": "A"}, {"id": "taskB", "priority": 0, "delay": 50, "result": "B"}, {"id": "taskC", "priority": 0, "delay": 150, "result": "C"}]}
Expected Output
["A", "B", "C"]
Case 4
Input
[
  1,
  [
    {"taskName": "Task A", "priority": 2, "delay": 100},
    {"taskName": "Task B", "priority": 1, "delay": 50},
    {"taskName": "Task C", "priority": 0, "delay": 20}
  ]
]
Expected Output
["Task C", "Task B", "Task A"]
Case 5
Input
{"maxConcurrency": 3, "tasks": [{"id": "t1", "priority": 2, "delay": 200, "result": "Low 1"}, {"id": "t2", "priority": 1, "delay": 100, "result": "Medium 1"}, {"id": "t3", "priority": 0, "delay": 50, "result": "High 1"}, {"id": "t4", "priority": 2, "delay": 180, "result": "Low 2"}]}
Expected Output
["High 1", "Medium 1", "Low 1", "Low 2"]
Case 6
Input
[
  3,
  [
    {"taskName": "T1", "priority": 0, "delay": 100},
    {"taskName": "T2", "priority": 0, "delay": 100},
    {"taskName": "T3", "priority": 0, "delay": 100}
  ]
]
Expected Output
["T1", "T2", "T3"]

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
6
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering

Languages

React NativeSvelteJavaScriptTypeScriptReactAngular