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
MediumJavaScript

Machine Coding: Build a Smart Task Scheduler with Cooldown

27 views

Problem Statement

In a high-intensity coding environment, "Context Switching" is the biggest productivity killer. You are tasked with building a Task Priority Scheduler that mimics a smart VS Code extension. The scheduler must manage tasks based on their urgency and estimatedTime, but it must also implement a "Cool Down" period to prevent developer burnout.

Implement a class TaskScheduler with the following requirements:

  1. addTask(task): Adds a task object { id, urgency, estimatedTime } to the queue.
  2. getNextTask(): Returns the task with the highest urgency. If two tasks have the same urgency, return the one with the shortest estimatedTime (Shortest Job First).
  3. completeTask(id): Marks a task as completed. After a task is completed, no new task can be started for a "Cool Down" period of 10ms per estimatedTime unit of the completed task.
  4. isAvailable(): Returns true if the developer is not in a cool-down period, false otherwise.

Example

const scheduler = new TaskScheduler();
scheduler.addTask({ id: 1, urgency: 5, estimatedTime: 100 });
scheduler.addTask({ id: 2, urgency: 5, estimatedTime: 50 });

console.log(scheduler.getNextTask().id); // 2 (Same urgency, shorter time)
scheduler.completeTask(2); 

console.log(scheduler.isAvailable()); // false (In 500ms cool down)
// After 500ms...
console.log(scheduler.isAvailable()); // true

Constraints

  • 0 <= urgency <= 10
  • estimatedTime is in milliseconds.
  • The scheduler should handle up to 1000 tasks.

Sample Test Cases

Case 1
Input
[
  {"method": "addTask", "args": [{"id": 1, "urgency": 5, "estimatedTime": 100}]},
  {"method": "addTask", "args": [{"id": 2, "urgency": 5, "estimatedTime": 50}]},
  {"method": "getNextTask", "args": []},
  {"method": "completeTask", "args": [2]},
  {"method": "isAvailable", "args": []}
]
Expected Output
[
  null,
  null,
  {"id": 2, "urgency": 5, "estimatedTime": 50},
  null,
  false
]
Case 2
Input
[
  {"method": "addTask", "args": [{"id": 1, "urgency": 5, "estimatedTime": 100}]},
  {"method": "addTask", "args": [{"id": 2, "urgency": 5, "estimatedTime": 50}]},
  {"method": "getNextTask", "args": []},
  {"method": "completeTask", "args": [2]},
  {"method": "isAvailable", "args": []}
]
Expected Output
[
  null,
  null,
  {"id": 2, "urgency": 5, "estimatedTime": 50},
  null,
  false
]
Case 3
Input
[
  {"method": "addTask", "args": [{"id": 1, "urgency": 1, "estimatedTime": 100}]},
  {"method": "addTask", "args": [{"id": 2, "urgency": 3, "estimatedTime": 50}]},
  {"method": "addTask", "args": [{"id": 3, "urgency": 2, "estimatedTime": 200}]},
  {"method": "getNextTask", "args": []},
  {"method": "completeTask", "args": [2]},
  {"method": "getNextTask", "args": []}
]
Expected Output
[
  null,
  null,
  null,
  {"id": 2, "urgency": 3, "estimatedTime": 50},
  null,
  {"id": 3, "urgency": 2, "estimatedTime": 200}
]
Case 4
Input
[
  {"method": "addTask", "args": [{"id": 1, "urgency": 1, "estimatedTime": 100}]},
  {"method": "addTask", "args": [{"id": 2, "urgency": 3, "estimatedTime": 50}]},
  {"method": "addTask", "args": [{"id": 3, "urgency": 2, "estimatedTime": 200}]},
  {"method": "getNextTask", "args": []},
  {"method": "completeTask", "args": [2]},
  {"method": "isAvailable", "args": []}
]
Expected Output
[
  null,
  null,
  null,
  {"id": 2, "urgency": 3, "estimatedTime": 50},
  null,
  false
]
Case 5
Input
[
  {"method": "isAvailable", "args": []},
  {"method": "addTask", "args": [{"id": 1, "urgency": 1, "estimatedTime": 10}]},
  {"method": "getNextTask", "args": []},
  {"method": "completeTask", "args": [1]},
  {"method": "isAvailable", "args": []},
  {"method": "_advanceTime", "args": [99]}, 
  {"method": "isAvailable", "args": []},
  {"method": "_advanceTime", "args": [1]}, 
  {"method": "isAvailable", "args": []}
]
Expected Output
[
  true,
  null,
  {"id": 1, "urgency": 1, "estimatedTime": 10},
  null,
  false,
  null,
  false,
  null,
  true
]
Case 6
Input
[
  {"method": "addTask", "args": [{"id": 1, "urgency": 10, "estimatedTime": 10}]},
  {"method": "completeTask", "args": [1]},
  {"method": "isAvailable", "args": []},
  {"method": "getNextTask", "args": []}
]
Expected Output
[
  null,
  null,
  false,
  null
]

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
27
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering

Languages

JavaScript

Skills

React