← Visa Interview Insights

Visa·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Visa SWE interview with a greedy optimization problem. Pretty clean question but the edge cases around when doubling actually helps took me a minute to think through properly.

Questions Asked (1)

Q1

Given two arrays of equal length and an integer k, where each index contributes min(a[i], b[i]) to a total sum, you can choose up to k indices to double b[i]. What strategy maximizes the total sum?

Algorithms & Data Structures
Author's notes

My first instinct was just to sort by the gain from doubling, which is the right direction, but I fumbled on figuring out the actual gain formula.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, recognize that doubling b[i] only increases the sum if b[i] < a[i], and the gain is a[i] - b[i]. Compute these gains for all indices, select up to k indices with the largest positive gains, and sum the original min(a[i], b[i]) plus these gains. This greedy strategy is optimal because each index's gain is independent and we can choose any subset of size up to k.

Pro tip: Mention that if k is larger than the number of positive gains, you simply take all positive gains; doubling indices with non-positive gain would decrease or not change the sum, so avoid them. Also, note that the solution runs in O(n log n) time due to sorting, which is efficient for large inputs.

1. Understand the effect of doubling

For each index i, determine how doubling b[i] changes the contribution to the sum. The new contribution becomes min(a[i], 2*b[i]), so the gain is min(a[i], 2*b[i]) - min(a[i], b[i]).

2. Simplify the gain condition

Show that the gain is positive only when b[i] < a[i], and in that case the gain equals a[i] - b[i]. If b[i] >= a[i], doubling b[i] does not change the contribution (gain = 0).

3. Compute and sort gains

Calculate the gain for each index, filter out non-positive gains, and sort the positive gains in descending order.

4. Select up to k indices

Take the top min(k, number of positive gains) gains and add them to the base sum of min(a[i], b[i]) over all indices.

5. Justify optimality

Explain that since each index's gain is independent and we can choose any subset of size up to k, the greedy choice of the largest gains maximizes the total sum.

Key Points to Mention

  • The gain from doubling b[i] is max(0, a[i] - b[i]).
  • Only indices where b[i] < a[i] can yield a positive gain.
  • Sorting the gains allows selecting the top k efficiently.
  • The greedy approach is optimal due to the independence of choices.
  • Time complexity is O(n log n) due to sorting, which is acceptable.
  • Edge cases: k=0, k >= number of positive gains, all gains non-positive.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.