Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
3,367 views

For proper oa or interview experiences list of all top tech companies visit :

https://oa.desiqna.in

https://interview.desiqna.in

 

image

 

in Online Assessments by Expert (44,360 points) | 3,367 views

3 Answers

0 like 0 dislike
Best answer

Task Queue
Task queues, which allow for asynchronous performance, are an important part of modern processing architectures. Information about a system consisting of several batch processing queues is given.

 

Each queue has three parameters:

 

  1. the maximum number of tasks it can process in a single batch.
  2. the time it takes to process a single batch of tasks in that queue
  3. the number of tasks the queue must process

 

Given this information. calculate the minimum time needed to process a set of tasks by the system.

 

Example
n = 2
batchSize = [4,3]
processingTime = [6,5]
numTasks = [8,8]

 

Queue 0 can process a max of 4 tasks in 6 minutes, and queue 1 can process a max of 3 tasks in 5 minutes. Each queue has 8 tasks to process. The time required to perform the assigned tasks in the minimu possible time is calculated as follows:

 

For queue 0:

 

  • At time = 0, a new batch of 4 tasks is initiated
  • At time = 6, the first batch of tasks is processed and a new batch of 4 tasks is initiated.
  • At time = 12, the second batch of tasks is processed. There are no more tasks left to process

 

For queue 1:

 

  • At time=0, a new batch of 3 tasks is initiated.
  • At time = 5, the first batch of tasks is processed and a new batch of 3 tasks is initiated
  • At time = 10, the second batch of tasks is processed and a new batch of 2 tasks is initiated.
    The min time to process all tasks is 15

 

Constraints:

 

  • 1 <= n <= 10^5
  • 1 <= batchSize[i] <= 10^9
  • 1 <= processingTime[i] <= 10^5
  • 1 <= numTasks[i] <= 10^9
by Expert (44,360 points)
0 like 0 dislike
 
Task Queue (Java Solution)


public static long minTime(List batchSize, List processingTime,List numTasks)
{


int n = batchSize.size();
long maxTime = Long.MIN_VALUE;


for(int i=0;i<n;i ++)
{
int t = numTasks.get(i)%batchSize.get(i);
int temp = numTasks.get(i)/batchSize.get(i) + (t == 0?0:1);
maxTime = Math.max(maxTime, temp*(long)processingTime.get(i));
}
return maxTime;
}

 
by Expert (44,360 points)
0 like 0 dislike

Solution: TC = O(n)

 

int minTime(std::vector<int> batchSize, 
         std::vector<int> processingTime, 
         std::vector<int> numTasks) 
{            
     int n = batchSize.size();
     int maxTime = INT_MIN;
     for(int i=0; i<n; i++)
     {
         int t = numTasks[i]%batchSize[i];
         int temp = numTasks[i]/batchSize[i] + (t == 0 ? 0 : 1);
         maxTime = std::max(maxTime, temp*processingTime[i]);
     }    
    return maxTime;
}
by Expert (44,360 points)