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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

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
Two Pointers
Sorting
Three Sum
Closest Value
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: list sizes with at least three integers, integer target.
  • 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 ...


Submit Your Answer
Markdown supported