Free Container Memory While Terminating the Fewest Processes
Last updated: November 29, 2026
Quick Overview
A Repl container's processes form a tree rooted at pid 1, each with a resident memory size, and killing a process terminates its whole subtree. Given a target amount of memory to free, choose kills (never pid 1) that reach the target while terminating the fewest total processes, and return that count or -1.
Replit
November 29, 20260
0
0 solved
A Repl container's processes form a tree rooted at pid 1, each with a resident memory size, and killing a process terminates its whole subtree. Given a target amount of memory to free, choose kills (never pid 1) that reach the target while terminating the fewest total processes, and return that count or -1.
When a Repl nears its memory limit, the platform would rather terminate a runaway subprocess tree than restart the container, and every terminated process is something the user may have to restart. The interviewer checks that you reject a ratio greedy, see the nested 0/1 knapsack, and write a tree DP with correct merging.
What the Interviewer Expects
- Shows a concrete case where a memory-per-process greedy fails
- Defines f[v][c] as the most memory freed terminating exactly c processes in v's subtree
- Merges children with a max-plus knapsack convolution
- Adds the whole-subtree option at index size(v)
- Uses an iterative traversal so deep process chains do not overflow the stack
- Argues the O(n^2) bound from subtree size merging
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 weight processes by importance (the user's dev server versus a stray file watcher) instead of counting them?
- How would you reconstruct which pids to kill?
- Could one DP run answer many different memory targets?
- What changes if processes are re-parented to pid 1 when their parent exits mid-decision?
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 (pid, ppid, rss) triples and need. Killing v frees the rss sum of its subtree and terminates size(v) processes. Kills must be in disjoint subtrees (killing inside a killed subtree adds not...
High-Level Design
Greedy by memory per process fails. Root children: A (1 process, 7 MB), B (2 processes totalling 10 MB, its child holds 6), C (2 MB), D (1 MB), need 10. Ratio greedy takes A, C, D for 3 processes; kil...