Skip to content

0/1 knapsack
one step at a time.

For every item and capacity, keep the better of skipping the item or taking it plus the best of what is left.

Free to use. Step forward and back at your pace, and share a link to the exact step you are stuck on.

What you actually watch.

The part a dry run on paper loses: the state, at every step.

For every item and capacity, keep the better of skipping the item or taking it plus the best of what is left. That sentence is easy to agree with and hard to hold in your head on step nine, which is where most people lose the thread.

So the visualiser keeps it in front of you: the call tree, and the table filling in as answers stop being recomputed. The line of code being executed is highlighted as the picture changes, so the drawing and the code never drift apart. Step forward, step back, or let it play.

Put your own input in. 0/1 knapsack on a clean example teaches you the shape; 0/1 knapsack on an empty input, a single element, or one with duplicates teaches you the thing an interviewer asks about.

How it runs, step by step.

The same order the visualiser moves in, so the list and the picture match.

  1. 1Break the problem into a smaller copy of itself.
  2. 2Solve the smallest cases directly: those are the base cases.
  3. 3Store each answer the first time it is computed.
  4. 4Reuse the stored answer instead of solving the same call twice.

Where people get this one wrong.

These are not made up. Each one is a switch in the tool: turn it on, run the same input, and watch the state go wrong.

  • Bug: dp[i][w − wt]Takes an item by reading its own row instead of the row above, so an item can go in again and again. Try "One item" with capacity 10.

Inputs worth trying.

The cases that teach something. Each one is a preset in the tool, with the reason it is there.

  • ClassicWatch row D at capacity 7: the most valuable item is left out.
  • Greedy trapA has the best value per weight, but B + C beat it.
  • Capacity 0One column: nothing can go in.
  • Nothing fitsEvery row copies the row above.
  • Everything fitsAll three go in. Then run the bug on it.
  • One item, lots of roomRun the dp[i][w − wt] bug on this one: it packs A three times.

What it costs.

The two numbers an interviewer will ask for, and what they mean here.

Time
O(n·W)counted as real operations while it runs, not quoted from a table
Space
O(n·W)what the algorithm holds besides the input itself
Pattern
Recursion & DPSolve smaller copies, then stop solving them twice.

Everything in the app, in one place.

Practice, interviews and the tools, all free to start. Swipe to see the rest.

Free to start: 100 AI credits when you sign up and 100 every month. Start free in the app

About 0/1 knapsack.

Open it in the app and the answers are easier to see than to read.

Open the visualiser
What does the 0/1 knapsack visualiser show?

Every step of the algorithm on input you choose: the call tree, and the table filling in as answers stop being recomputed, with the line of code being executed highlighted beside it. You can step forward, step back, or let it play. Spelled visualizer or visualiser, it is the same tool.

What is the time complexity of 0/1 knapsack?

O(n·W) time and O(n·W) space. The visualiser counts the real operations as it runs, so you can watch the number grow instead of taking the formula on trust.

Why does my 0/1 knapsack code fail on some inputs?

Usually one of the known traps. The visualiser ships the real ones for this algorithm (dp[i][w − wt]), so you can switch the bug on, run it, and watch exactly where the state goes wrong.

Can I use my own input?

Yes. Type your own array, string or graph and the algorithm runs on that, including the edge cases that break a solution in an interview.

Is it free?

Yes. The app is free to start, with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.