CTC 2024 Intern OA Question - What is Optimized Solution?

4094
Aug 29, 2023
Aug 29, 2023

There are N clients who have ordered N handmade items. The K-th client ordered exactly one item that takes T[K] hours to make. There is only one employee who makes items for clients, and he/she works in the following manner:

  • spend 1 hour making the first item
  • if the item is finished, deliver it
  • if the item is NOT finished, put it after ALL REMAINING ITEMS for futher work
  • employee then works on next item

What is the total time that clients need to wait for all ordered items?

Example: T = [3, 1, 2]

In this example, the 1st item takes 3 hours to make, the 2nd item takes 2 hours, and the 3rd item takes 2 hours. Here is how the employee goes about finishing all items: [1st, 2nd, 3rd, 1st, 3rd, 1st].

It ended up taking 6 passing hours to finish the first item, 2 passing hours the second item, and 5 passing hours for the third item. Summing all this up, we get the answer of 13.

An efficient solution should be O(nlogn). However, I am not sure the code for this efficient solution so if anyone could share I would appreciate it.

Comments (14)