FastPrep0/1 Knapsack
Problems/0/1 Knapsack

0/1 Knapsack

Amazon logoAmazon● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Amazon full-time onsite interview. The report names a well-known problem. We chose the function signature, a fixed order for the output, and the input limits. Our version is about 98% the same as the reported question.

You are given two arrays, weights and values, describing n items. Item i has weight weights[i] and value values[i].

Choose a subset of the items whose total weight is at most capacity. Each item may be chosen at most once. Return the maximum total value achievable.

Function

maximumKnapsackValue(weights: int[], values: int[], capacity: int) → int

Examples

Example 1

weights = [1,3,4,5]values = [1,4,5,7]capacity = 7return = 9

Choosing the items with weights 3 and 4 uses the full capacity and yields value 4 + 5 = 9.

Example 2

weights = [2,2,3]values = [6,10,12]capacity = 5return = 22

The second and third items fit together and yield value 22.

Example 3

weights = [4,5]values = [8,9]capacity = 3return = 0

No item fits, so the empty subset is optimal.

Constraints

  • 0 <= weights.length = values.length <= 200
  • 1 <= weights[i] <= 10000
  • 0 <= values[i] <= 1000000
  • 0 <= capacity <= 10000
  • The maximum answer fits in a signed 32-bit integer.

Source note: Author-written prompt excerpt retained for source fidelity.

More Amazon Onsite Interview questions

  • Next Greater Element IONSITE INTERVIEW · Seen Dec 2026
  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
  • Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
  • Optimal Account BalancingONSITE INTERVIEW · Seen Dec 2026
  • Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Flood FillONSITE INTERVIEW · Seen Nov 2026
  • Merge k Sorted ListsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
  • Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Distinct Subset SumsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
  • Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions
  • Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
  • Coin ChangeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Maximum SubarrayUpstart · ONSITE INTERVIEWUpstart interview questions
  • Smallest Sufficient TeamExpedia · ONSITE INTERVIEWExpedia interview questions
  • Maximum Product SubarrayHartford Financial Services · ONSITE INTERVIEWHartford Financial Services interview questions
  • Word BreakMoveworks · ONSITE INTERVIEWMoveworks interview questions
  • Maximum Value from Circular HousesDigitalOcean · ONSITE INTERVIEWDigitalOcean interview questions

Interview reports that mention this question

  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II (L5) Interview Questions: 5 Experiences

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
See Amazon hiring insights
Saved
class Solution {
    public int maximumKnapsackValue(int[] weights, int[] values, int capacity) {
        // Write your solution here
    }
}
Checking account…