Problem Statement: Optimize Task Scheduling for a Data Center

You are developing a task scheduling system for a data center that handles various computational tasks. Each task has a unique priority and an estimated execution time. Your goal is to implement a system that efficiently schedules tasks based on their priority, ensuring that the highest priority tasks are executed first.

Implement a Java program that uses a heap data structure to manage the scheduling of tasks. Your program should be able to insert tasks, extract the highest priority task for execution, and adjust the heap when tasks are completed or new tasks are added.

Requirements:

  1. Functionality:
  • Implement a Task class to represent each task, containing a priority and an execution time.
  • Implement a TaskScheduler class that uses a max heap to manage tasks.
  • Provide methods to:
    • Add a new task.
    • Extract the task with the highest priority.
    • Adjust the heap when tasks are executed or new tasks are added.
  1. Constraints:
  • The system should handle up to (10^6) tasks.
  • Task priorities are integers ranging from 1 (lowest) to 1000 (highest).
  • Execution times are integers between 1 and 10000 milliseconds.
  1. Interface:
  • Your TaskScheduler class should include the following methods
public class Task{
    public int getPriority();
    public int getExecutionTime();
}
  • The Task class should have following methods
public class TaskScheduler {
   public void addTask(int priority, int executionTime);
   public Task extractHightestPriorityTask();
}

Example:

Certainly! Here’s the expected output for the two example scenarios with 10 inputs each:

Example 1 Expected Output:

Input Operations:

scheduler.addTask(5, 3000);
scheduler.addTask(10, 1000);
scheduler.addTask(3, 2000);
scheduler.addTask(8, 1500);
scheduler.addTask(1, 5000);
scheduler.addTask(7, 1200);
scheduler.addTask(2, 4000);
scheduler.addTask(9, 1800);
scheduler.addTask(4, 2500);
scheduler.addTask(6, 1000);

Extraction Output:

10
9
8
7
6
5
4
3
2
1

Example 2 Expected Output:

Input Operations:

scheduler.addTask(50, 500);
scheduler.addTask(20, 300);
scheduler.addTask(80, 800);
scheduler.addTask(70, 700);
scheduler.addTask(40, 400);
scheduler.addTask(60, 600);
scheduler.addTask(10, 100);
scheduler.addTask(90, 900);
scheduler.addTask(30, 200);
scheduler.addTask(100, 1000);

Extraction Output:

100
90
80
70
60
50
40
30
20
10

Objective:
Ensure that tasks are managed and scheduled efficiently based on their priority using a heap data structure.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top