Mid From PDF Coding C# Coding Interview

0/1 Knapsack Problem?

Short answer: int Knapsack(int[] weights, int[] values, int W) { int n = weights.Length; int[,] dp = new int[n + 1, W + 1]; for (int i = 1; i <= n; i++) { for (int w = 1; w <= W; w++) { if (weights[i - 1] <= w) dp[i, w] = Math.Max(dp[i - 1, w], values[i - 1] + dp[i - 1, w - weights[i - 1]]); else dp[i, w] = dp[i - 1, w]; } } return dp[n, W]; } Explanation: Build table where dp[i,w] = max value using first i items and capacity w.

Example code

int Knapsack(int[] weights, int[] values, int W)
{
int n = weights.Length;
int[,] dp = new int[n + 1, W + 1];
for (int i = 1; i <= n; i++)
{
for (int w = 1; w <= W; w++)
{
if (weights[i - 1] <= w) dp[i, w] = Math.Max(dp[i - 1, w], values[i - 1] + dp[i - 1, w - weights[i - 1]]); else dp[i, w] = dp[i - 1, w];
}
}
return dp[n, W];
} Explanation: Build table where dp[i,w] = max value using first i items and capacity w. Follow on:

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details