FastPrepCargo Port Ship Stops
Problems/Cargo Port Ship Stops

Cargo Port Ship Stops

Nutanix logoNutanix● EasyINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This was the first of two questions in a 90-minute, fully recorded Nutanix intern online assessment in January 2020. A ship has a limit on both item count and total weight. It takes cargo in its original order, delivers it to destination ports, and comes back. The task is to count the stops, including the stop back at the home port, not to find the fewest possible. The report also warns that the two input arrays can differ in length.

The report does not say how stops at the same port are counted or what to do with the extra entries. Here each trip stops once per different port. Only positions present in both arrays are items. Every item fits alone. The limits, the signature and the examples are our own.

Our version is about 85% the same as the reported question.

Cargo items wait in a line at a home port. Item i weighs weights[i] and must be delivered to port ports[i]. The delivery ports are numbered from 1 to portCount.

A ship carries the items in trips. On each trip:

  1. The ship loads items from the front of the line, one at a time and in order. It keeps loading while it holds fewer than maxItems items and the next item keeps the total weight at most maxWeight. Loading stops at the first item that does not fit; no later item is loaded ahead of it.
  2. The ship stops once at each different port that its items go to. Several items for the same port need only one stop there.
  3. The ship returns to the home port. This counts as one more stop.

Trips go on until the line is empty. Return the total number of stops.

The arrays weights and ports may have different lengths. Only the first k positions describe items, where k is the length of the shorter array. Extra values at the end of the longer array are ignored.

Function

countShipStops(weights: int[], ports: int[], portCount: int, maxItems: int, maxWeight: int) → int

Examples

Example 1

weights = [40,25,60,10,30]ports = [2,1,2,3,1]portCount = 3maxItems = 3maxWeight = 100return = 7

Trip 1 loads 40 and 25 (total 65). The next item weighs 60, and 65 + 60 = 125 is more than 100, so the ship leaves. It stops at ports 2 and 1, then returns home: 3 stops. Trip 2 loads 60, 10 and 30 (total exactly 100, and 3 items). It stops at ports 2, 3 and 1, then returns home: 4 stops. The answer is 3 + 4 = 7.

Example 2

weights = [5,5,5,5]ports = [1,1,2]portCount = 2maxItems = 2maxWeight = 50return = 4

ports has 3 values and weights has 4, so only the first 3 items exist; the last weight is ignored. Trip 1 loads the first two items (the item limit is 2). Both go to port 1, so it is 1 stop plus the return: 2 stops. Trip 2 loads the third item for port 2: 1 stop plus the return, 2 stops. The answer is 4.

Example 3

weights = [9,9,9]ports = [1,1,1]portCount = 1maxItems = 3maxWeight = 10return = 6

Two items together weigh 18, more than 10, so every trip carries one item. Each trip stops at port 1 and returns home: 2 stops. Three trips give 6 stops. Port 1 is counted again on every trip that visits it.

Constraints

  • 1 <= weights.length <= 10^4
  • 1 <= ports.length <= 10^4
  • 1 <= portCount <= 10^4
  • 1 <= ports[i] <= portCount
  • 1 <= maxItems <= 10^4
  • 1 <= weights[i] <= maxWeight <= 10^9
  • The two arrays may have different lengths.

More Nutanix Online Assessment questions

  • Good SubsequencesOA · Seen Jul 2026
  • Min Cost to Connect All PointsOA · Seen Jun 2026
  • Digit Changes for a Multiple of 9OA · Seen Aug 2025
  • Gas Station Loop StartOA · Seen Aug 2025
  • Number of Players (for MTS2)OA · Seen Oct 2024
  • Work Schedules (for MTS2)OA · Seen Oct 2024
  • Minimum Operations to Convert ArrayOA · Seen Apr 2024
  • No Adjacent CharactersOA · Seen Apr 2024
  • Build a BST From a ListOA · Seen Nov 2022
  • Delete Even-Valued Nodes From a Linked ListOA · Seen Nov 2022

Similar questions at other companies

  • Basic Item DiscountsPalantir · OAPalantir interview questions
  • Calculate Portfolio Rebalancing DeltasAkuna Capital · OAAkuna Capital interview questions
  • Filter Transaction Logs and Calculate Reliability MetricsStripe · OAStripe interview questions
  • Get Total RequestsAmazon · OA
  • Balloon Color PairsInstacart · OAInstacart interview questions
  • Command Frequency CounterUber · OAUber interview questions
  • Parcel Attribute Store with TTLAirbnb · OAAirbnb interview questions
  • Process Banking OperationsVercel · OA
  • First Completed Mingo LineEpic · OAEpic interview questions
  • Count House Segments After Sequential DestructionByteDance · OAByteDance interview questions

Interview reports that mention this question

  • Nutanix SWE Intern OA Questions: 16 Experiences (Part 2 of 3)

Nutanix interview experiences

  • Nutanix SWE Intern Phone Screens: 20 Experiences (Part 3 of 3)
  • Nutanix U.S. SWE Intern Interviews: 54 Experiences (Part 1 of 3)
  • Nutanix SWE OA and Interviews, Level Not Reported: 9 Experiences
  • Nutanix Intern Debugging Round and Interviews: 25 Experiences
All Nutanix interview questionsSee Nutanix hiring insights
Saved
class Solution {
    public int countShipStops(int[] weights, int[] ports, int portCount, int maxItems, int maxWeight) {
        // Write your code here.
    }
}
Checking account…