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
Coding & Algorithms
Software Engineer
Replit
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
tree DP
knapsack
process trees
memory pressure
post-order traversal
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 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 Problems
Sample 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...


Submit Your Answer
Markdown supported

Related Questions