Bird
0
0
DSA Typescriptprogramming~20 mins

Unbounded Knapsack Problem in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unbounded Knapsack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Unbounded Knapsack DP Table
What is the final maximum value returned by this unbounded knapsack function for the given inputs?
DSA Typescript
function unboundedKnapsack(W: number, wt: number[], val: number[], n: number): number {
  const dp: number[] = new Array(W + 1).fill(0);
  for (let i = 0; i <= W; i++) {
    for (let j = 0; j < n; j++) {
      if (wt[j] <= i) {
        dp[i] = Math.max(dp[i], dp[i - wt[j]] + val[j]);
      }
    }
  }
  return dp[W];
}

const W = 7;
const wt = [1, 3, 4];
const val = [10, 40, 50];
const n = wt.length;
console.log(unboundedKnapsack(W, wt, val, n));
A100
B90
C120
D80
Attempts:
2 left
💡 Hint
Think about how many times you can pick the item with weight 1 and value 10 to maximize value.
🧠 Conceptual
intermediate
1:30remaining
Understanding Unbounded Knapsack Item Choices
In the unbounded knapsack problem, why can items be chosen multiple times?
ABecause the problem allows unlimited quantity of each item to maximize value within weight limit.
BBecause items are duplicated in the input list to simulate multiple quantities.
CBecause the knapsack capacity is infinite, so all items fit.
DBecause the problem only considers the first occurrence of each item.
Attempts:
2 left
💡 Hint
Think about the difference between 0/1 knapsack and unbounded knapsack.
🔧 Debug
advanced
2:00remaining
Identify the Error in This Unbounded Knapsack Code
What error will this TypeScript code produce when run?
DSA Typescript
function unboundedKnapsack(W: number, wt: number[], val: number[], n: number): number {
  const dp: number[] = new Array(W + 1).fill(0);
  for (let i = 1; i <= W; i++) {
    for (let j = 0; j < n; j++) {
      if (wt[j] < i) {
        dp[i] = Math.max(dp[i], dp[i - wt[j]] + val[j]);
      }
    }
  }
  return dp[W];
}

console.log(unboundedKnapsack(5, [5], [100], 1));
AInfinite loop occurs
BReturns 90 correctly
CReturns 0
DThrows runtime error: dp[i - wt[j]] is undefined
Attempts:
2 left
💡 Hint
Check the condition inside the inner loop carefully.
📝 Syntax
advanced
1:30remaining
Syntax Error in Unbounded Knapsack Code
Which option contains a syntax error preventing the code from compiling?
DSA Typescript
function unboundedKnapsack(W: number, wt: number[], val: number[], n: number): number {
  const dp: number[] = new Array(W + 1).fill(0);
  for (let i = 0; i <= W; i++) {
    for (let j = 0; j < n; j++) {
      if (wt[j] <= i) {
        dp[i] = Math.max(dp[i], dp[i - wt[j]] + val[j]);
      }
    }
  }
  return dp[W];
}
Afor (let i = 0; i <= W; i++) {
Bif (wt[j] <= i) {
Cconst dp: number[] = new Array(W + 1).fill(0);
Ddp[i] = Math.max(dp[i], dp[i - wt[j]] + val[j])
Attempts:
2 left
💡 Hint
Look for missing punctuation in the code lines.
🚀 Application
expert
3:00remaining
Maximum Value for Large Capacity with Given Items
Given items wt = [2, 3, 5], val = [50, 70, 100], and knapsack capacity W = 10, what is the maximum value achievable using unbounded knapsack?
A250
B300
C270
D200
Attempts:
2 left
💡 Hint
Try combinations of items that fill the capacity exactly or nearly exactly with maximum value.