Design a vending machine
54326

Problem description:
Design a vending machine.

Clarify:
No more restrictions, just design a general vending machine we are familiar with.

Use case list:

  • select item and get price
  • accept bills/coins
  • dispense items purchased and return change
  • refund when cancelling the request

Possible exceptions:

  • Sold out
  • Not fully paid
  • Not enough changes

Design pattern:
Someone proposed state design pattern, but I am not familiar with it. Refer to this post.

Draw UML diagrams:

  • Vending Machine class. It has cash, items fields to store the current state of the machine. Also it should have currentBalance, currentItem fields.
  • Item class. To store items to be sold. Should have price field.
  • Bills class. To store bill type.
  • … Maybe draw whatever you think of and leave it for interviewer's reference.

A more detailed UML diagram:
0_1500077526210_class-diagram.png

For detaild code, refer to this post, which gives nice analysis and complete code on vending machine problem.

Besides above question, I would like to share some general approach towards OOD problem:

  • Clarify: many OOD questions are intentionally vague for us to clarify.
  • Use case: an easy way to get started. List all functions we need to implement, then analysis what should the object do and how should it behave.
  • Propose a design pattern: not necessary but a bonus point.
  • Draw a diagram: draw all parts of the object to make it functional. Keep in mind the OOD concepts. This is a very important step to show our ideas, draw as clear as we can. (One funny tip I learned is, in case we suddenly forgot everything in an actual interview, draw the exact object we are asked to design first to get our brain warm!)
  • Analysis the constrains: consider more use cases, what possibly make the object unfunctional and how can we fix it. Or consider the data size, what is the key constrains for the object to behave efficiently.
  • Scalability: how can we scale for larger data set. This is more of system design part, it is good to mention but be careful on followups.
  • Good to keep in mind: The most important part of OOD interview is how we think through a problem. Usually, process is more important than actually answer, and there is no absolute right or wrong answers. So try to analysis as much as we can with the interviewer.
Comments (10)