0
0
DSA Typescriptprogramming~20 mins

Fractional Knapsack Problem in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fractional Knapsack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Fractional Knapsack Value Calculation
What is the output of the following TypeScript code that calculates the maximum value for the fractional knapsack problem?
DSA Typescript
interface Item {
  value: number;
  weight: number;
}

function fractionalKnapsack(items: Item[], capacity: number): number {
  items.sort((a, b) => (b.value / b.weight) - (a.value / a.weight));
  let totalValue = 0;
  let remainingCapacity = capacity;

  for (const item of items) {
    if (item.weight <= remainingCapacity) {
      totalValue += item.value;
      remainingCapacity -= item.weight;
    } else {
      totalValue += item.value * (remainingCapacity / item.weight);
      break;
    }
  }
  return totalValue;
}

const items = [
  { value: 60, weight: 10 },
  { value: 100, weight: 20 },
  { value: 120, weight: 30 }
];
const capacity = 50;
console.log(fractionalKnapsack(items, capacity));
A220
B180
C200
D240
Attempts:
2 left
💡 Hint
Sort items by value-to-weight ratio and take as much as possible from the highest ratio items first.
🧠 Conceptual
intermediate
1:30remaining
Understanding Fractional Knapsack Greedy Choice
Why does the fractional knapsack problem use a greedy approach based on value-to-weight ratio?
ABecause taking items with the highest value-to-weight ratio first maximizes total value for the given capacity.
BBecause it tries all combinations of items to find the best solution.
CBecause it sorts items by weight only to minimize total weight.
DBecause it selects items randomly to fill the knapsack quickly.
Attempts:
2 left
💡 Hint
Think about which items give the most value per unit weight.
🔧 Debug
advanced
2:00remaining
Identify the Error in Fractional Knapsack Implementation
What error will the following TypeScript code produce when run?
DSA Typescript
interface Item {
  value: number;
  weight: number;
}

function fractionalKnapsack(items: Item[], capacity: number): number {
  items.sort((a, b) => (a.value / a.weight) - (b.value / b.weight));
  let totalValue = 0;
  let remainingCapacity = capacity;

  for (const item of items) {
    if (item.weight <= remainingCapacity) {
      totalValue += item.value;
      remainingCapacity -= item.weight;
    } else {
      totalValue += item.value * (remainingCapacity / item.weight);
      break;
    }
  }
  return totalValue;
}

const items = [
  { value: 60, weight: 10 },
  { value: 100, weight: 20 },
  { value: 120, weight: 30 }
];
const capacity = 50;
console.log(fractionalKnapsack(items, capacity));
AOutputs 240
B220
CThrows a runtime error due to division by zero
DOutputs 200
Attempts:
2 left
💡 Hint
Check the sorting order and how it affects the selection of items.
📝 Syntax
advanced
1:30remaining
Syntax Error in Fractional Knapsack Code
Which option contains a syntax error that will prevent the TypeScript code from compiling?
DSA Typescript
interface Item {
  value: number;
  weight: number;
}

function fractionalKnapsack(items: Item[], capacity: number): number {
  items.sort((a, b) => (b.value / b.weight) - (a.value / a.weight));
  let totalValue = 0;
  let remainingCapacity = capacity;

  for (const item of items) {
    if (item.weight <= remainingCapacity) {
      totalValue += item.value;
      remainingCapacity -= item.weight;
    } else {
      totalValue += item.value * (remainingCapacity / item.weight);
      break;
    }
  }
  return totalValue;
}
AIncorrect interface declaration for Item
BWrong parameter types in fractionalKnapsack function
CMissing semicolon after totalValue += item.value * (remainingCapacity / item.weight)
DUsing 'let' instead of 'const' for totalValue
Attempts:
2 left
💡 Hint
Look carefully at the line inside the else block.
🚀 Application
expert
2:30remaining
Maximum Value with Fractional Knapsack for Custom Items
Given the following items and knapsack capacity, what is the maximum value the fractional knapsack algorithm will return?
DSA Typescript
interface Item {
  value: number;
  weight: number;
}

const items: Item[] = [
  { value: 500, weight: 30 },
  { value: 400, weight: 20 },
  { value: 300, weight: 10 },
  { value: 450, weight: 25 }
];
const capacity = 50;
A1060
B950
C850
D1150
Attempts:
2 left
💡 Hint
Calculate value-to-weight ratios and take items starting from the highest ratio until capacity is full.