Magicsheet logo

Minimum Amount of Time to Collect Garbage

Medium
12.5%
Updated 8/1/2025

Asked by 2 Companies

Minimum Amount of Time to Collect Garbage

What is this problem about?

The Minimum Amount of Time to Collect Garbage problem features a neighborhood where different houses have three types of garbage: Metal (M), Paper (P), and Glass (G). You have three specialized trucks, one for each type. Each truck starts at house 0 and must collect all garbage of its specific type. You are given the amount of garbage at each house and the travel time between consecutive houses. The goal is to find the total time spent by all three trucks combined.

Why is this asked in interviews?

This is a standard Minimum Amount of Time to Collect Garbage interview question at companies like Microsoft and Amazon. It tests a candidate's ability to simplify a seemingly complex simulation. While it sounds like you need to simulate three separate routes, the core logic relies on identifying the last house each truck needs to visit. It evaluates efficiency in iterating through arrays and managing cumulative sums.

Algorithmic pattern used

The Prefix Sum interview pattern and a "Last Occurrence" tracking strategy are most effective here.

  1. Total time = (Total units of garbage) + (Total travel time for each truck).
  2. Every unit of garbage takes 1 minute to collect, regardless of the house.
  3. For each truck type (M, P, G), identify the index of the last house that contains that type of garbage.
  4. The travel time for a truck is the prefix sum of travel times up to its last house index.

Example explanation

Garbage: ["G", "P", "GP", "GG"], Travel: [2, 4, 3].

  1. Total garbage units: 1 'G' (house 0) + 1 'P' (house 1) + 1 'G', 1 'P' (house 2) + 2 'G' (house 3) = 6 units. (6 mins).
  2. Last house for 'G': house 3. Travel time = 2 + 4 + 3 = 9.
  3. Last house for 'P': house 2. Travel time = 2 + 4 = 6.
  4. Last house for 'M': None. Travel time = 0.
  5. Total = 6 (collection) + 9 (G travel) + 6 (P travel) = 21 minutes.

Common mistakes candidates make

  • Simulating truck by truck: Writing three separate loops that do unnecessary work. A more experienced candidate will realize they can find all "last indices" in one pass.
  • Forgetting that trucks don't return: Some candidates add the travel time back to house 0, but the trucks only need to reach their last stop.
  • Redundant calculations: Recalculating the travel sum repeatedly instead of using a prefix sum array or a running total.

Interview preparation tip

When a problem involves multiple "agents" (like trucks) moving along the same path, ask if their actions are independent. Here, the trucks don't interfere with each other, so you can calculate their costs separately and sum them up. This Array interview pattern is common in logistics-style questions.

Similar Questions