Minimize travel cost with two cities
Company: Bloomberg
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: Evaluates algorithmic skills in cost-optimization, greedy reasoning and sorting-based assignment while requiring correct handling of constraints and edge cases. Commonly asked in the Coding & Algorithms domain to test a candidate’s ability to design and implement a concrete, implementation-level algorithm and reason about correctness and complexity.
Constraints
- 1 <= n == len(c1) == len(c2) <= 200000
- 0 <= c1[i], c2[i] <= 10^9
Examples
Input: ([30], [5])
Expected Output: 30
Explanation: There is only one candidate, and ceil(1/2) = 1, so that candidate must go to New York even though San Francisco is cheaper.
Input: ([10, 30, 400, 30], [20, 200, 50, 20])
Expected Output: 110
Explanation: Send candidates 0 and 1 to New York, and candidates 2 and 3 to San Francisco. Total = 10 + 30 + 50 + 20 = 110.
Input: ([50, 20, 70], [30, 100, 40])
Expected Output: 110
Explanation: n = 3, so ceil(3/2) = 2 candidates must go to New York. The cheapest valid assignment is candidates 0 and 1 to New York, candidate 2 to San Francisco: 50 + 20 + 40 = 110.
Input: ([5, 5, 5, 5, 5], [5, 5, 5, 5, 5])
Expected Output: 25
Explanation: All costs are identical, so any valid assignment gives the same total cost of 25.
Input: ([100, 100], [1, 1])
Expected Output: 101
Explanation: Exactly one of the two candidates must go to New York. The total is 100 + 1 = 101.
Hints
- Try first assuming that everyone goes to San Francisco. Then ask: what is the extra cost or savings if a candidate is switched to New York?
- For candidate i, that switch changes the total by c1[i] - c2[i]. Which candidates should be chosen for New York?