Incremental Memory Leak - Problem

Imagine you're managing a computer system with two memory sticks that are being consumed by a faulty program. This program has a peculiar behavior: it consumes an increasing amount of memory each second!

You start with two memory sticks containing memory1 and memory2 bits respectively. At the i-th second (starting from second 1), the program tries to allocate exactly i bits of memory. The allocation always goes to the memory stick with more available memory, or to the first stick if both have equal memory.

The program will crash when neither memory stick has enough bits to satisfy the current allocation request. Your task is to determine exactly when this crash occurs and how much memory remains in each stick.

Goal: Return [crashTime, memory1crash, memory2crash] where crashTime is when the program crashed, and the other values are the remaining memory in each stick.

Input & Output

example_1.py โ€” Basic Case
$ Input: memory1 = 2, memory2 = 2
โ€บ Output: [3, 1, 0]
๐Ÿ’ก Note: Second 1: Both have 2 bits, choose memory1 โ†’ memory1=1, memory2=2. Second 2: memory2 > memory1, choose memory2 โ†’ memory1=1, memory2=0. Second 3: Need 3 bits, but memory1=1 and memory2=0, so crash at second 3.
example_2.py โ€” Unequal Memory
$ Input: memory1 = 8, memory2 = 11
โ€บ Output: [6, 0, 4]
๐Ÿ’ก Note: Sec 1: Choose memory2 (11โ‰ฅ8) โ†’ [8,10]. Sec 2: Choose memory2 (10โ‰ฅ8) โ†’ [8,8]. Sec 3: Choose memory1 (equal, pick first) โ†’ [5,8]. Sec 4: Choose memory2 (8โ‰ฅ5) โ†’ [5,4]. Sec 5: Choose memory1 (5โ‰ฅ4) โ†’ [0,4]. Sec 6: Need 6 bits but max available is 4, crash.
example_3.py โ€” Large Memory
$ Input: memory1 = 100, memory2 = 1
โ€บ Output: [15, 85, 1]
๐Ÿ’ก Note: Memory2 will be eliminated early since it's small. After that, memory1 will be consistently chosen until it can't satisfy the allocation. The mathematical optimization approach really shines here.

Constraints

  • 0 โ‰ค memory1, memory2 โ‰ค 231 - 1
  • The program always starts from second 1
  • Memory allocation always goes to the stick with more memory, or to memory1 if equal

Visualization

Tap to expand
Tank 1Tank 2ControlSystemHour: 1Drain: 1LActive drainInactive
Understanding the Visualization
1
Hour 1
Drain 1 liter from fuller tank (or first if equal)
2
Hour 2
Drain 2 liters from fuller tank
3
Hour 3
Drain 3 liters from fuller tank
4
Pattern Recognition
If one tank is much fuller, it gets drained for many consecutive hours
5
System Failure
When neither tank can supply the required amount
Key Takeaway
๐ŸŽฏ Key Insight: When memory values differ significantly, we can use mathematical formulas to calculate how many consecutive allocations will go to the larger memory stick, dramatically reducing simulation time from O(โˆšn) to O(logโˆšn).
Asked in
Amazon 45 Google 32 Microsoft 28 Apple 20
89.2K Views
Medium Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen