Fewest Two-Slot Instances to Host Paired Microservices
Last updated: November 29, 2026
Quick Overview
Each instance can host at most two services whose combined memory does not exceed a limit; given service memory sizes, return the minimum number of instances needed.
IBM
November 29, 20260
0
0 solved
Each instance can host at most two services whose combined memory does not exceed a limit; given service memory sizes, return the minimum number of instances needed.
IBM Cloud Code Engine and virtual server sizing questions often reduce to pairing items under a limit. In IBM's online assessment this greedy two-pointer problem checks sorting, the pairing argument, and clean loop termination.
What the Interviewer Expects
- Sort sizes
- Pair the largest remaining with the smallest if they fit
- Justify why the greedy is optimal
- Handle a single remaining service
- Run in O(n log n)
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
- What if each instance can host up to three services?
- How would you return the actual pairs?
- Why does pairing the two largest that fit fail to be optimal?
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 mem of positive integers, each at most limit. Output: minimum number of instances, where each holds one or two services with total memory at most limit....
High-Level Design
Sort ascending. The largest service must go somewhere; the best partner for it is the smallest service, because if even the smallest does not fit, nothing does, and if it fits, using it leaves larger ...