#352 Calculating Total Customer Dissatisfaction

Companies:
Avito
Themes:
AlgorithmsBinary SearchPractice

Avito contains many products, each represented by a number. Every customer has a product need, also represented by a number. If there is no exact product, the customer chooses the product with the closest value, causing dissatisfaction equal to the difference between the need and the purchased product. The quantity of every product is unlimited, and the same product may be purchased by several customers.

Calculate the total dissatisfaction of all customers.

Write a function that accepts two arrays: an array of products and an array of customer needs, calculates the total dissatisfaction of all customers, and returns the result as a number.

# Example
# input
goods = [8, 3, 5]
buyerNeeds = [5, 6]

# output
res = 1 # the first customer buys product 5 and has dissatisfaction 0; the second also buys product 5 and has dissatisfaction 6-5 = 1

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in