Panda Guru LogoPanda
Guru

Salesforce | MTS | OA

Round 1

Questions: A customer has posted several web development projects on a freelancing platform, and various web developers have put in bids for these projects. Given the bid amounts and their corresponding projects, what is the minimum amount the customer can pay to have all the projects completed?

Note: If any project has no bids, return -1.

Example numProjects = 3 projects. projectId = [2, 0, 1, 2] bid = [8, 7, 6, 9].

There is only one choice of who to hire for project 0, and it will cost 7. Likewise, there is only one choice for project 1, which will cost 6. For project 2, it is optimal to hire the first web developer, instead of the fourth, and doing so will cost 8. So the final answer is 7 + 6 + 8 = 21.

If instead there were n = 4 projects, the answer would be -1 since there were no bids received on the fourth project.

Function Description Complete the function minCost in the editor below.

minCost has the following parameters:

Returns: long: the minimum cost the client can spend to complete all projects, or -1 if any project has no bids.

Constraints:

Sample Case 0 Sample Input

STDIN Function ----- ----- 2 → numProjects = 2 5 → projectId[] size n = 5 0 → projectId = [0, 1, 0, 1, 1] 1 0 1 1 5 → bid[] size n = 5 4 → bid = [4, 74, 47, 744, 7] 74 47 744 7

Sample Output

11

Explanation The bids are as follows:

The optimal solution is to hire the first web developer to complete project 0 (which costs 4) and to hire the fifth web developer to complete project 1 (which costs 7). So the total cost for the customer will be 4 + 7 = 11.

Working Solution

public static int minimumAmountToPay(int numProjects, int[] projectid, int[] bid) { // Map to store the minimum bid for each project Map<Integer, Integer> minBidMap = new HashMap<>(); // Process each bid for (int i = 0; i < projectid.length; i++) { int project = projectid[i]; int bidAmount = bid[i]; // Update the minimum bid for this project if (minBidMap.containsKey(project)) { minBidMap.put(project, Math.min(minBidMap.get(project), bidAmount)); } else { minBidMap.put(project, bidAmount); } } // Calculate the total minimum amount int totalMinAmount = 0; for (int project = 0; project < numProjects; project++) { if (!minBidMap.containsKey(project)) { // If any project has no bids, return -1 return -1; } totalMinAmount += minBidMap.get(project); } return totalMinAmount; }
Candidate's Approach

No approach provided.

Interviewer's Feedback

No feedback provided.