I came across this question in an interview recently for the SDE-2 position.

There is a shop with old-style cash registers. the price of each item is typed manually. Given a list of items and their correct prices, compare the prices to those entered when each item was sold. Determine the number of errors in the selling price.

products = ['eggs','milk','cheese']
productPrices = [2.89,3.29,5.79]
productsSold = ['eggs','eggs','milk','cheese']
sellingPrices = [2.89,2.99,3.29,5.97]
Output: errors = 2

Here Comes the Solution for the same.

Use 2 HashMap, one for Storing the original product name and product price, HashMap<String, Float> map2 = new HashMap<String, Float>(); and another for Storing the Selling product name and product price, HashMap<String, ArrayList<Float>> map = new HashMap<String, ArrayList<Float>>(); Here we took Values as a list because there may be multiple selling prices for the same product.

Here is the code for the same.

public static int priceCheck(List<String> products, List<Float> productPrices, List<String> productSold, List<Float> soldPrice) {
HashMap<String,ArrayList<Float>> map = new HashMap<String,ArrayList<Float>>();

for(int i=0;i<productSold.size();i++){
String pSold = productSold.get(i);
ArrayList<Float> list = map.get(pSold);
map.put(pSold, list);
ArrayList<Float> list = new ArrayList<Float>();
HashMap<String,Float> map2 = new HashMap<String,Float>();
for(int i=0;i<products.size();i++){
map2.put(products.get(i), productPrices.get(i));

int error = 0;
for(Map.Entry<String,Float> map3:map2.entrySet()){
String pName = map3.getKey();
Float pCost = map3.getValue();
ArrayList<Float> price = map.get(pName);
for(int i=0;i<price.size();i++){
Float sCost = price.get(i);
return error;

The code is self-explanatory!

Happy Coding!

Software Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store