Max Profit with Capacity (Fractional Knapsack with Dependencies)

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-08-25

Problem Statement

Design and implement a system that solves a profit maximization problem under capacity constraints, similar to the Fractional Knapsack, but with the added complexity of item dependencies (i.e., some items can only be taken if their prerequisites are also taken).


This is not just about writing an algorithm — the focus is on class design, clean code, extensibility, and handling edge cases.


Functional Requirements


1.Inputs
•A list of Items, each having:
•id (unique identifier)
•weight
•profit
•dependencies (list of item IDs that must be chosen before this item)
•A maximum capacity (knapsack size).
2.Outputs
•A set of chosen items (with possible fractions of items, like fractional knapsack).
•Maximum profit achievable under given constraints.
3.Constraints
•Items can be partially taken (fractions allowed).
•Dependencies must be satisfied before an item is picked.
•If capacity is exhausted, the system should stop.
•Handle cycles in dependencies gracefully (invalid input).


Example


Input:

1 2Capacity = 50 3Items: 41: weight=10, profit=60, dependencies=[] 52: weight=20, profit=100, dependencies=[] 63: weight=30, profit=120, dependencies=[1]


Processing:


•Profit/weight ratios: (1 → 6, 2 → 5, 3 → 4).
•Item 1 can be taken directly.
•Item 3 can only be taken if Item 1 is taken.
•Best solution: Take Item 1 (10,60), Item 2 (20,100), remaining capacity 20 → take 20/30 of Item 3 = (20,80).


Output:

1 2Items Taken: [1 (10/10), 2 (20/20), 3 (20/30 fraction)] 3Max Profit = 240


Non-Functional Requirements


•Clean extensible design: future extension to support 0/1 knapsack, or grouped dependencies.
•Correctness: dependency enforcement must be strict.
•Efficiency: handle large inputs (n up to 10^5 items).
•Thread-safety: if multiple threads may compute solutions in parallel on shared data.


Class Design (Java-like Sketch)

1 2class Item { 3 String id; 4 double weight; 5 double profit; 6 List<String> dependencies; 7 8 double getProfitPerWeight() { 9 return profit / weight; 10 } 11} 12 13class KnapsackResult { 14 Map<String, Double> itemsTaken; // itemId -> fraction taken 15 double totalProfit; 16} 17 18class FractionalKnapsackWithDependencies { 19 Map<String, Item> items; 20 double capacity; 21 22 public FractionalKnapsackWithDependencies(List<Item> items, double capacity) { 23 this.items = new HashMap<>(); 24 for (Item i : items) this.items.put(i.id, i); 25 this.capacity = capacity; 26 } 27 28 public KnapsackResult maximizeProfit() { 29 // 1. Topologically sort items based on dependencies 30 // 2. Sort items by profit/weight ratio (within dependency constraints) 31 // 3. Greedily pick items while respecting capacity 32 // 4. Allow fractional picks 33 } 34}


Key Design Discussion Points for LLD Round


•Dependency Resolution: topological sort to ensure valid ordering.
•Greedy Strategy + Dependencies: how profit/weight interacts with dependency graph.
•Edge Cases:
•Cyclic dependencies → invalid input.
•Item with dependency heavier than capacity.
•Fractional inclusion of dependent items.
•Extensibility: Could extend to:
•Grouped Knapsack (pick whole group or none).
•Multi-Knapsack (multiple capacities).


Solution

Comments