Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: Which statement correctly contrasts 0/1 knapsack and fractional knapsack?

  1. A. Both are solved optimally by the same greedy algorithm
  2. B. 0/1 knapsack is solved by greedy, fractional needs DP
  3. C. Neither can be solved in polynomial time
  4. D. Fractional knapsack is solved optimally by a greedy value/weight-ratio approach, but the same greedy strategy is not optimal for 0/1 knapsack, which needs DP

Correct answer: D. Fractional knapsack is solved optimally by a greedy value/weight-ratio approach, but the same greedy strategy is not optimal for 0/1 knapsack, which needs DP

When items can be split, taking the highest value/weight ratio first is always optimal (fractional knapsack). Once items must be taken whole, that greedy choice can be suboptimal, so 0/1 knapsack requires O(nW) DP over weight capacity.

More Coding Interview Problems questions

Exams that ask this topic