Fill a Cache Tier With Divisible Objects to Maximize Hit Value
Last updated: November 29, 2026
Quick Overview
A cache tier has fixed capacity in megabytes, and each candidate object has a size and an expected hit value; objects may be cached partially with value proportional to the fraction stored. Return the maximum total value.
0
0
0 solved
A cache tier has fixed capacity in megabytes, and each candidate object has a size and an expected hit value; objects may be cached partially with value proportional to the fraction stored. Return the maximum total value.
Fractional knapsack is a quick check that a Google candidate knows when greedy is optimal and when it is not. Interviewers often follow up by removing divisibility to see if you recognize the switch to dynamic programming.
What the Interviewer Expects
- Sort by value density (value per MB) descending
- Take whole objects while they fit, then a fraction of the next
- Explain why density order is optimal only for divisible items
- Contrast with 0/1 knapsack
- O(n log n) time
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
- Objects cannot be split. What algorithm now?
- Capacity is huge and values update often; maintain the answer.
- Some objects must be cached together.
- Do it in O(n) using selection instead of sorting.
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: capacity (float or int) and list of (size, value) with positive sizes. Output: the maximum total value as a float, allowing fractions....
High-Level Design
Every megabyte of capacity should hold the most valuable megabyte available. Sort objects by value / size descending. Take each object fully while it fits; when one does not fit, take the fraction `...