Magicsheet logo

Campus Bikes II

Medium
25%
Updated 8/1/2025

Campus Bikes II

What is this problem about?

The Campus Bikes II interview question is a more difficult version of the first assignment problem. Instead of a greedy assignment, you need to find the assignment of bikes to workers that minimizes the total Manhattan distance sum. You are given n workers and m bikes (m >= n). This Campus Bikes II coding problem is a classic optimization task that can't be solved greedily.

Why is this asked in interviews?

Google uses this to evaluate a candidate's mastery of Dynamic Programming and Bitmasking. Because you need to find the absolute minimum across all possible combinations, simple sorting won't work. It tests your ability to represent the state of "available bikes" using a bitmask and how to use recursion with memoization to avoid redundant calculations.

Algorithmic pattern used

This utilizes the Array, Bitmask, Dynamic Programming interview pattern.

  • State: dp(worker_index, bike_mask), representing the minimum distance to assign bikes to workers from worker_index to n-1, given that the bikes represented by the bitmask are already taken.
  • Transition: For the current worker, try assigning every bike that is not yet set in the bitmask, and recurse.

Example explanation

Worker 0, Worker 1. Bike 0, Bike 1.

  • Try (W0, B0) + (W1, B1). Total = dist1.
  • Try (W0, B1) + (W1, B0). Total = dist2. The algorithm explores these paths and stores the minimum. If there were 10 workers and 10 bikes, the number of permutations is 10!, but DP with bitmasking reduces the state space to N * 2^M.

Common mistakes candidates make

  • Trying Greedy: Assuming that the closest pairs will lead to the best total. This is a common fallacy in optimization problems.
  • Exponential Complexity: Not using memoization, which causes the solution to recalculate the same "remaining bikes" sub-problems repeatedly.
  • Incorrect Bitwise ops: Forgetting how to check or set a bit in the mask (mask & (1 << i) or mask | (1 << i)).

Interview preparation tip

Master Bitmask DP. It is the standard solution for problems involving "assigning N things to M things" when N and M are small (usually <= 15-20$). If the constraints are small, bitmasking is almost always the intended path.

Similar Questions