Interview Question

Senior Software Engineer Interview San Mateo, CA

Support dependent costs between categories. Please

  reference the question mentioned above.
Answer

Interview Answer

1 Answer

0

Store each loaded category in a Category class, which holds the names of the choices, and the current choice, accessed with setChoice(String) and getChoice(). Maintain a Map<String, Category> mapNameToCategory from category names to the Category instances representing them. To encode the business rules, use a Map<Condition, Integer> mapConditionToCost, where the Integer is the cost and the Condition class is constructed as Condition(String category, String choice, String dependentCategory, String dependentChoice) and has the obvious equality / hash overrides.

An example entry in the conditions map is, if the milk creamer costs 60 cents with an XL size, add a map entry with Condition(“size”, “xl”, “creamer”, “milk”) and Integer(60). The client specifies these conditions in a text file with lines like “size xl creamer milk 60”. The same syntax can be used to set default costs: “default default creamer milk 50”. To assure the integrity of the string references, the setup file could instead be an XML with Schema validation.

To figure the cost given the current choices in each category, iterate through the Categorys using mapNameToCategory. For each category, get the default cost for the selection with:

mapConditionToCost.get(new Condition(“default”, “default”, category.getName(), category.getChoice()));

Then iterate through each other category and check if there are any special conditions recorded in the map given the other category’s choice:

mapConditionToCost.get(new Condition(other.getName(), other.getChoice(), category.getName(), category.getChoice()));

This is an n-squared process of course, but that hardly matters with the number of categories you’d expect to have an a cofee vending machine. Alternatively though, you could iterate through the conditions map checking each one to see if it’s currently true. This would be on the order of the number of conditions. You might also partition the conditions map into each Category, or even create a Choice class and partition the conditions map further into each choice. That way, you would only have to check the conditions involving that Category/Choice.

Interview Candidate on Aug 26, 2010

Add Answers or Comments

To comment on this question, Sign In with Facebook or Sign Up