Problem · Array

Distribution Center Package Allocation

Learn this problem
MediumTiktok logoTiktokINTERNOA
See Tiktok hiring insights

Problem statement

You manage a network of distribution centers indexed from 0 to n - 1. Center i can process centerCapacities[i] packages before it needs a reset.

Process the entries of dailyLog in order. Every entry is one of:

  • "PACKAGE": a package arrives for processing.
  • "CLOSURE j": center j closes and remains unavailable for the rest of the process.

Packages are assigned to centers in increasing index order, beginning at center 0. A center handles consecutive packages until its remaining capacity reaches 0, then allocation advances to the next available center. Closed centers are always bypassed.

When allocation passes the end of the center array, one complete rotation has finished. Restore every operational center to its original capacity, continue from index 0, and keep bypassing closed centers. If a center closes while it still has unused capacity, the next package advances to the next available center.

Return the index of the center that processed the most packages. If several centers processed the same maximum number, return the highest index among them.

Function

mostPackagesProcessed(centerCapacities: int[], dailyLog: String[]) → int

Examples

Example 1

centerCapacities = [2,1,3]dailyLog = ["PACKAGE","PACKAGE","PACKAGE","PACKAGE","CLOSURE 2","PACKAGE","PACKAGE","PACKAGE"]return = 0

Before the closure, centers 0, 1, and 2 process 2, 1, and 1 packages. Center 2 then closes. The next rotation assigns two packages to center 0 and one to center 1, so the totals are [4, 2, 1] and center 0 wins.

Example 2

centerCapacities = [1,1,1]dailyLog = ["PACKAGE","PACKAGE","PACKAGE"]return = 2

Each center processes exactly one package. The totals are tied, so the highest center index, 2, is returned.

Example 3

centerCapacities = [3,2,1]dailyLog = ["PACKAGE","CLOSURE 0","PACKAGE","PACKAGE","PACKAGE","PACKAGE"]return = 1

Center 0 processes one package and then closes with capacity remaining. Center 1 processes the next two packages, center 2 processes one, and the final package starts a new rotation at center 1. The totals are [1, 3, 1].

Constraints

  • 1 <= centerCapacities.length
  • 1 <= centerCapacities[i] <= 5
  • Every entry of dailyLog is either "PACKAGE" or "CLOSURE j" for a valid center index j.
  • At least one center remains operational throughout the process.

More Tiktok problems

drafts saved locally
public int mostPackagesProcessed(int[] centerCapacities, String[] dailyLog) {
    // Write your code here.
}
centerCapacities[2,1,3]
dailyLog["PACKAGE","PACKAGE","PACKAGE","PACKAGE","CLOSURE 2","PACKAGE","PACKAGE","PACKAGE"]
expected0
checking account