← Bloomberg Interview Insights
My first instinct was brute force and I actually said that out loud which, fine, but I should've pivoted faster.
Start by clarifying the problem constraints and edge cases, then propose a greedy solution: compute the cost difference (NY - SF) for each candidate, sort by this difference, assign the ceil(n/2) candidates with the smallest differences to NY and the rest to SF. Finally, discuss unit tests covering n=1, odd n, and cases where all differences are equal or extreme.
Pro tip: Mention that this greedy approach is optimal because swapping any two candidates across cities would increase total cost, and explicitly state the time complexity O(n log n) due to sorting. Also, proactively discuss how to handle ties in differences and verify with a brute-force check for small n.
Confirm the exact number of candidates going to NY (ceil(n/2)) and SF (floor(n/2)), and discuss edge cases like n=1, n=0, and odd n. Ask if costs are non-negative integers or can be floats.
For each candidate, compute the difference d_i = cost_NY_i - cost_SF_i. Sorting by d_i ascending and assigning the first ceil(n/2) to NY minimizes total cost because it minimizes the sum of differences for NY-assigned candidates.
Argue that any other assignment would require swapping a NY-assigned candidate with a SF-assigned candidate, which would increase total cost by d_j - d_i >= 0 since d_i <= d_j. Thus the greedy assignment is optimal.
Write code that sorts candidates by difference and sums costs accordingly. State time complexity O(n log n) and space O(n) (or O(1) extra if sorting in place).
List test cases: n=1 (only NY), n=2 (one each), n=3 (two NY, one SF), n=4 (two each), all differences equal, all differences positive/negative, and large random cases compared against brute force for small n.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.