FastPrepGumball Machines
Problems/Gumball Machines

Gumball Machines

Nutanix logoNutanix● EasyINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This question came from a Nutanix intern online assessment in early 2020. It is a restocking simulation. A diner keeps three gumball machines, each with one color and at most 1000 gumballs. Over at most 20 days, a daily truck color either tops up a matching machine, goes into an empty one, or pushes out the machine with the fewest gumballs. Machines sell 10 a day at one cent each. You total what is thrown away.

The report does not say how the machines start, which empty machine is used, or whether sales come before or after the morning truck. It also prices gumballs in cents while asking for dollars. Here all machines start empty, the truck comes first, and you return the total in cents. Under these rules the empty-machine choice and the order of truck and sales never change the answer. The color format and the examples are our own.

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

A diner has three gumball machines. Each machine holds gumballs of a single color, at most 1000 of them. All three machines start empty.

You are given colors, where colors[i] is the color of gumballs a truck brings on the morning of day i. Each morning the machines are restocked with these rules, checked in order:

  1. If a machine already holds this color, fill it back up to 1000. Nothing is thrown away.
  2. Otherwise, if a machine is empty, fill it with 1000 gumballs of the new color.
  3. Otherwise all three machines hold other colors. Throw away every gumball in the machine that holds the fewest, then fill that machine with 1000 gumballs of the new color.

After the restock, every machine that holds gumballs sells 10 of them during the day. Each gumball is worth 1 cent.

Return the total value, in cents, of all gumballs thrown away over all the days.

With these rules, in step 3 exactly one machine holds the fewest gumballs, and in step 2 it does not matter which empty machine you use.

Function

discardedCents(colors: String[]) → int

Examples

Example 1

colors = ["red","blue","green","pink"]return = 970

Days 0, 1 and 2 fill the three empty machines with red, blue and green. On the morning of day 3 they hold 970 red (sold on days 0, 1 and 2), 980 blue and 990 green. Pink matches none of them and no machine is empty, so the 970 red gumballs are thrown away and that machine gets 1000 pink. The answer is 970 cents.

Example 2

colors = ["mint","lime","plum","mint","cola","lime"]return = 1940

Days 0 to 2 fill the machines with mint, lime and plum. On day 3 mint comes again: its machine goes back up to 1000 and nothing is thrown away. On the morning of day 4 the machines hold 990 mint, 970 lime and 980 plum, so cola replaces lime and 970 gumballs are thrown away. On the morning of day 5 they hold 980 mint, 990 cola and 970 plum. Lime is no longer in a machine, so plum is replaced and 970 more are thrown away. The answer is 970 + 970 = 1940 cents.

Example 3

colors = ["gold","gold","gold"]return = 0

Gold fills one machine on day 0 and refills the same machine on days 1 and 2. Nothing is ever thrown away, so the answer is 0.

Constraints

  • 1 <= colors.length <= 20
  • 1 <= colors[i].length <= 10
  • colors[i] consists only of lowercase English letters.

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

  • Count Server ReplacementsAkuna Capital · OAAkuna Capital interview questions
  • Basic Item DiscountsPalantir · OAPalantir interview questions
  • Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions
  • Student and Course EnrollmentWells Fargo · OAWells Fargo interview questions
  • Network Endpoint or Cycle BoundaryOpenAI · OA · PHONE SCREENOpenAI interview questions
  • Branch with the Most FilesCapital One · OACapital One interview questions
  • Assign Server Numbers by TypeOura · OA
  • Filter Transaction Logs and Calculate Reliability MetricsStripe · OAStripe interview questions
  • Calculate Net ProfitJP Morgan Chase · OAJP Morgan Chase interview questions
  • Get Total RequestsAmazon · OA

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 discardedCents(String[] colors) {
        // Write your code here.
    }
}
Checking account…