# 0/1 Knapsack: maximize value without exceeding capacity

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/2279/maximize-knapsack-value
Source observed: 2026-11-04T21:42:48.819Z
Last checked: 2026-11-04T21:42:48.819+00:00
Indexed: 2026-11-05T03:39:49.05+00:00

---

# 0/1 Knapsack: maximize value without exceeding capacity

**Difficulty:** Medium  
**Interview count:** 1  
**Category:** Data Structures & Algorithms  
**Last asked:** 4 years ago  
**Role:** [Software Engineer](https://www.aced.io/questions?role=swe)  
**Company:** [Uber](https://www.aced.io/questions?company=uber)  
**Topics:** [Data Structures & Algorithms](https://www.aced.io/questions?type=algorithms), [Coding](https://www.aced.io/questions?type=coding), [Technical](https://www.aced.io/questions?type=technical)

We ask you to implement a function for the **0/1 Knapsack** problem. Given items with weights and values, choose which items to include so that the total weight is less than or equal to the knapsack's maximum capacity `C`, while maximizing total value. Each item can be selected at most once.

## Inputs

Your function takes:

1. A list of weights, `w[i]`, representing the weight of each item.
2. A list of values, `v[i]`, representing the value of each item.
3. An integer, `C`, representing the knapsack's maximum capacity.

Return the maximum total value that fits within capacity `C`. The solution must not exceed the knapsack's maximum capacity.

## Examples

```text
weights = [10, 20, 30]
values = [60, 100, 120]
capacity = 50
output: 220

weights = [1, 1, 1]
values = [10, 20, 30]
capacity = 2
output: 50

weights = [4, 2, 3]
values = [10, 20, 15]
capacity = 5
output: 35

weights = [4, 5, 1]
values = [1, 2, 3]
capacity = 4
output: 3
```

## Available practice views

The question interface includes **Question**, **Solution**, **Mock Interview**, and **Community** views. The Solution and Mock Interview views are locked in the captured interface. The community area shows 12 entries in the interface, while no community answers have been contributed for this question.

You can select Python in the editor, view the **Output** or **Test Results** panels, and use the console. Running code requires [signing up](https://www.aced.io/signup?src=lesson).

## Related question

[Given a list of items with different sizes, write an algorithm to pack these items into the minimum number of batches without exceeding the capacity of each batch.](https://www.aced.io/questions/4910/batch-packing-bin-optimization)

## Related courses

### [Software Engineering Interview Prep](https://www.aced.io/courses/software-engineering)

![Software Engineering Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/4VkuYewBHUFIVUS5oJDW5h/02e833439b1289f25f38f13f81cbdca3/adobe_stock_optimized.jpeg?w=800&h=800&fm=webp&q=75)

We help you prepare for software engineering roles at Google, Amazon, Microsoft, Meta, Apple, and other top companies through mock interviews, frameworks, senior-candidate advice, data structures, algorithms, system design, people management, and behavioral interview practice.

### [System Design Interviews](https://www.aced.io/courses/system-design-interviews)

![System Design Interviews](https://images.ctfassets.net/x78yjrjc11pq/78x7miFZFjBCrM5GqftrMJ/2f6d26e632c4e7265c49f6a180227edb/system_design_track_compressed_26ac98a46e__281_29_5be2280f31.jpeg?w=800&h=800&fm=webp&q=75)

We teach approaches for answering system design questions across product, infrastructure, and AI domains, with in-depth video examples, written breakdowns, reference patterns, and mock interview videos featuring senior engineers and managers.

[Upgrade](https://www.aced.io/upgrade?src=questions) to unlock detailed company statistics for this question.

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F2279%2Fmaximize-knapsack-value&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F2279%2Fmaximize-knapsack-value). Opening a form does not execute a tool.