Skip to main content
All Questions

Find the minimum cost to connect fromServer[i] to toServer[i] for each query.

Unlock detailed company stats for this questionUpgrade

There are n servers with distinct capacities in the array capacity. The distance between servers i and j is |capacity[i] - capacity[j]|; each server's closest server is unique. Operations on server x: connect to any server y at cost |capacity[x] - capacity[y]|, or connect to the closest server of x for a fixed cost of 1 unit. Connections can be direct or routed through other servers. Complete the function getMinCost(int capacity[n], int fromServer[m], int toServer[m]) returning int[m]: the minimum cost per query. Constraints: 1 <= n, m <= 2*10^5; 1 <= capacity[i] <= 10^9. Example: capacity = [2, 7, 10], queries 0->2, 1->2, 2->1 -> [2, 1, 1]. Sample: capacity = [2, 3, 5, 6], fromServer = [0, 2, 0], toServer = [3, 0, 1] -> [4, 3, 1].

Format · Microsoft OA.

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.