Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    The smallest van that finishes in time

    MediumAbout 25 minFull AccessLearn it: Binary Search in C#: Lower Bound, Array.BinarySearch, Rotated Arrays and Searching the Answer

    The task

    Parcels leave a warehouse in the order given. Each day the van takes the next run of consecutive parcels whose total weight is at most its capacity; it never splits a parcel and never skips ahead.

    Complete MinDailyCapacity(int[] weights, int days). Return the smallest capacity that sends every parcel within days days. Every weight is at least 1, 1 ≤ days ≤ weights.Length, and the total weight fits in an int.

    Signature

    public static int MinDailyCapacity(int[] weights, int days)

    Examples

    • [4, 8, 2, 6, 5], days = 2 → 13. 4 + 8 on the first day and 2 + 6 + 5 = 13 on the second; with 12 the van needs a third day.
    • [1, 2, 3], days = 1 → 6. Everything goes on one day.

    Limits: Up to 200,000 parcels; the total weight can be close to int.MaxValue.

    The tests check: one parcel, one parcel a day, an even split, a heavy parcel first or last (the capacity is never below the heaviest parcel), totals near int.MaxValue, and 200,000 parcels in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // Returns the smallest van capacity that sends all parcels, in order, within the given days.
          public static int MinDailyCapacity(int[] weights, int days)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.