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
Coding & Algorithms
Software Engineer
IBM
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

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
greedy
two pointers
sorting
pairing
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
  • 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported

Related Questions