Skip to main content
All Questions

Compute the total cost of allocating machine instances to M customers.

Unlock detailed company stats for this questionUpgrade

Given an array with the number of instances available for each machine type, M customers arrive one by one. Each customer is allocated an instance from the machine type with the most available instances. The customer's cost is (maximum instances before allocation) + (minimum non-zero instances currently present). After allocation that machine's count drops by 1. Continue until all customers are processed or instances run out, and return the total cost. Example: instances = [1, 3, 2, 4]: customer 1 sees max=4, min=1, cost=5, and the array becomes [1, 3, 2, 3].

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.