← Questions
Split tasks between two workers when one takes exactly k
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's online assessment, last asked Oct 2025. It tests greedy and sorting.
There are n tasks and two workers. Worker 1 earns reward1[i] for doing task i, and worker 2 earns reward2[i]. Every task goes to exactly one worker, and worker 1 must do exactly k tasks; worker 2 does the rest. Return the largest possible total reward.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
max_reward(reward1: list[int], reward2: list[int], k: int) -> int.
Example
max_reward([5, 2, 3, 8], [1, 4, 6, 3], 2) -> 23 # worker 1 takes tasks 0 and 3: 5 + 8 + 4 + 6
max_reward([5, 2, 3, 8], [1, 4, 6, 3], 0) -> 14 # worker 2 does everything
max_reward([5, 2, 3, 8], [1, 4, 6, 3], 4) -> 18 # worker 1 does everything
Constraints
-
1 <= n <= 10^6,0 <= k <= n. - Rewards are integers and may be negative; the total fits in 64 bits.
Clarifying questions
- Is "exactly
k" strict, or at mostk? - Can rewards be negative?
- Should I also return which tasks worker 1 gets?
- Is
nlarge enough to rule out a DP over tasks and counts?
What a strong answer covers
- The exchange argument: start with every task on worker 2, then moving task
ito worker 1 changes the total byreward1[i] - reward2[i], independently of the other tasks. Pick theklargest differences. - Sorting the differences, O(n log n), or a selection algorithm (quickselect, or a heap of size
k) in O(n) average. - Why a DP over tasks and counts, O(n · k), works but is far too slow at this size.
- Tests:
k = 0,k = n, ties in the differences, negative rewards.
Follow-ups
- Worker 1 must do at least
aand at mostbtasks. - Return the chosen task indices in increasing order.
- Three workers, each with an exact quota. Does the greedy still work? (No; what would you use instead?)
Where it's asked
- Uber · Online assessment · Last asked Oct 202590 min · Two problems on a HackerRank-based platform, camera and screen share on · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
