FastPrep0/1 Knapsack
Problems/0/1 Knapsack

0/1 Knapsack

Microsoft logoMicrosoft● MediumFULLTIMEINTERNOAONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Microsoft online assessment for full-time and intern roles. The original report names the classic 0/1 knapsack task. It leaves out the function interface, the examples, and the limits. We filled those in.

Our version is about 94% 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.

More Microsoft Online Assessment questions

  • Maximum Score with Prime JumpsOA · Seen Dec 2026
  • Minimum Non-Adjacent Selection CapabilityOA · Seen Dec 2026
  • Authentication SystemOA · Seen Nov 2026
  • Maximum String OperationsOA · Seen Nov 2026
  • Minimum Cost to Process Priority TasksOA · Seen Nov 2026
  • Most Visited Sprint MarkerOA · Seen Nov 2026
  • Maximum Escape Game ScoreOA · Seen Nov 2026
  • Count Subarrays with a Perfect-Square ProductOA · Seen Nov 2026
  • Maximum Interactive Team SizeOA · Seen Nov 2026
  • Maximize Pipeline Throughput Under a Scaling BudgetOA · Seen Nov 2026

Similar questions at other companies

  • Maximum Sum with K-Element SkippingPhonePe · ONSITE INTERVIEWPhonePe interview questions
  • Maximum Beauty by DeletionsMeesho · OA
  • Card Game: Best First-Player ScoreShopify · OAShopify interview questions
  • Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
  • Distinct Subset SumsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Maximum Game Score with Fixed Forward JumpsStripe · OAStripe interview questions
  • Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
  • 0/1 KnapsackConfluent · OAConfluent interview questions
  • Minimum Cost to Tile a Strip with Widths 1, 2, and 3Infosys · OAInfosys interview questions
  • Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions

Interview reports that mention this question

  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern Interviews: 244 Experiences (Part 1 of 6)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int maximumKnapsackValue(int[] weights, int[] values, int capacity) {
        // Write your solution here
    }
}
Checking account…