Pick Three VM Sizes Whose Total Is Closest to a Target Capacity
Last updated: November 29, 2026
Quick Overview
Given available VM memory sizes and a target capacity, choose three different VMs whose total memory is as close as possible to the target and return that total.
0
0
0 solved
Given available VM memory sizes and a target capacity, choose three different VMs whose total memory is as close as possible to the target and return that total.
Framed around bin-packing workloads onto Google Compute Engine hosts, this is a sort-then-two-pointers problem. The interviewer checks the O(n^2) reduction from three nested loops and the pointer movement rule driven by the sign of the error.
What the Interviewer Expects
- Sorts first and fixes one element per outer iteration
- Moves inner pointers based on whether the sum is low or high
- Tracks the closest sum by absolute difference
- Returns immediately on an exact match
- States O(n^2) time and O(1) extra space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the closest sum that does not exceed the target?
- Generalize to choosing k VMs.
- What if n is a million and sizes come from a small set of machine types?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list
sizeswith at least three integers, integertarget. - Output: the sum of three distinct positions closest to
target(any one if tied)....
High-Level Design
Sort the sizes. For each index i, run two pointers on the suffix: lo = i + 1, hi = n - 1. Compute s = sizes[i] + sizes[lo] + sizes[hi]. If s is closer than the best so far, record it. If `s ...