This programming challenge is asked in the Amazon coding round hosted on the Amcat platform.
There will be two coding questions to be solved in 90 minutes. So you can take 45 minutes to solve this coding challenge.
In this tutorial, I’m explaining one coding program. Another coding program, you can find here- the minimum distance required for the truck to deliver the order.
Problem Statement: Minimum Cost of Merging Files
Amazon has a cloud music application. When the user subscribes to it, music files are encoded and then transfer it to the user’s smartphone. The encoder takes each music file and merges them all into a single file. This single file will be sent to the user’s smartphone.
At a time, the encoder can only merge two files. The cost of merging two files is the same as the size of those two files. Find the minimum cost to merge all files into one.
Input format:
Output format:
Write a function to the return minimum cost required to compress files.
Input: [14, 25, 5, 8 ] Output: 92
Explanation:
First, two files of size 5 and 8 will be merged. It will cost 13 (5+8). The updated files on the list are [14, 25, 13].
In the next turn, two files of size 13 and 14 will be merged. It will cost 27 (13+14). The updated files in the list are [25, 27]
Finally, 25 and 27 will merge to [52], it will cost 52.
The total cost to merge all the files is 92 (13+27+52).
It has a simple solution.
You can do it with and without recursion.
Before going through the below code, I would recommend you write your own code. If you are stuck anywhere, you can always refer to the below code.
With recursion:
def minCostToMerge(files): if len(files)<=1: return 0 else: minA=min(files) files.remove(minA) minB=min(files) files.remove(minB) files.append(minA+minB) return minA+minB+minCostToMerge(files) print(minCostToMerge([14, 25, 5, 8 ]))
Output:
92
Without recursion (using while loop):
def minCostToMerge(files): cost=0 while(len(files)>1): minA=min(files) files.remove(minA) minB=min(files) files.remove(minB) files.append(minA+minB) cost= cost+minA+minB return cost print(minCostToMerge([21, 17, 5, 9]))
Output:
97
To solve this kind of problem, you have to be very good at handling a Python list. You should aware of all the methods to add, delete, and update the element in the list.
Your Challenge…
Can you solve this problem to calculate the minimum cost of merging files in any other programming language like C/C++, Java…?
Please share your code with me in the comment. I will add it to this challenge.
Sir, there is a minute correction in this code inside condition of loop…
while(len(files) > 1):
Thanks
Thanks for the correction, Moksha! Fixed it.
What is the runtime complexity?
We have to traverse the array to find the two smallest elements which will take
O(n)
times. We are iterating over the complete array to find the two smallest elements and merge them.– In the first iteration, complexity will be
O(n)
.– After merging two elements, the total elements in the array will be
(n-1)
So the complexity in the second iteration will beO(n-1)
– And so on…
Total complexity will be
O(n) + O(n-1) + O(n-2)+...+1
which is equavalent toO(n^2)
.What is the runtime complexity?
Please check above comment.
What are time and space complexity?
I have explained the time complexity above which is
O(n^2)
. We are not taking any other extra space, so the space complexity is constant i.e.O(1)
.How can I post a question to you, sir? So that you can explain what I missed…
Btw, thank you for the code, sir. It helped a lot…
Bhargav, I’m glad this tutorial helped you. You can always ask me the questions in the comment section. We also have a Facebook group if you would like to join.