← Questions

Split tasks between two workers when one takes exactly k

0
UberCodingLast asked Oct 2025

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 most k?
  • Can rewards be negative?
  • Should I also return which tasks worker 1 gets?
  • Is n large 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 i to worker 1 changes the total by reward1[i] - reward2[i], independently of the other tasks. Pick the k largest 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 a and at most b tasks.
  • 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

greedy · sorting

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