Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Vending Machine Internals

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Vending Machine Internals. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Program a vending machine internals to accept and return cash coins.

Implement process_vending_machine(denominations, inventory, inserted, price). Return a dictionary containing accepted coins, rejected coins, change, refund, and the updated inventory. Unsupported coins are rejected. A transaction succeeds only when accepted value reaches price and the machine can make exact change from its inventory plus accepted coins. If the transaction cannot complete, refund all supported inserted coins and leave inventory unchanged.

Inputs use integer denominations, a string-keyed inventory mapping denomination to count, an inserted-coin list, and an integer price. Return change in descending order and preserve inserted order for accepted and rejected coins.

Example: [10], price 6, and four available 1 coins returns change [1,1,1,1]. If [5] is inserted for a price of 10, it is refunded.

Constraints: at most 20 denominations, at most 100 inserted coins, nonnegative counts and prices, and valid inventory keys for listed denominations.

Constraints

  • 1 <= len(denominations) <= 20
  • 0 <= len(inserted) <= 100
  • 0 <= inventory[denomination] <= 100
  • 0 <= price
  • All denominations are distinct positive integers
  • Inventory keys are the string forms of listed denominations

Function Signature

def process_vending_machine(denominations, inventory, inserted, price):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output