FastPrepMaximum Weight in Two Bags
Problems/Maximum Weight in Two Bags

Maximum Weight in Two Bags

Apple logoApple● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Apple full-time online assessment. The report names this exercise and links to its details. Our version is about 99% the same as the reported question.

You have items with weights weights and two bags with capacities w1 and w2. Each item may go into the first bag, into the second bag, or remain unpacked.

An item cannot be split or used in both bags. Neither bag may exceed its capacity. Return the greatest combined weight of the packed items.

Function

maxWeight(weights: int[], w1: int, w2: int) → int

Examples

Example 1

weights = [1,4,3,2]w1 = 5w2 = 4return = 9

Put weights 3 and 2 in the first bag and weight 4 in the second.

Example 2

weights = [3,6,4,8]w1 = 9w2 = 7return = 15

Pack 8 in the first bag and 3 + 4 in the second.

Example 3

weights = [5,7]w1 = 2w2 = 3return = 0

Neither item fits in either bag.

Constraints

  • 1 <= weights.length <= 100.
  • 1 <= weights[i] <= 100.
  • 1 <= w1, w2 <= 300.

More Apple interview questions

  • Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Merge Two Strings by Maximum Boundary OverlapOA · Seen Sep 2026
  • Collect Letters Along a Unique Grid PathOA · Seen Aug 2025
  • Ignore SectionsOA · Seen Mar 2024
  • Gas Station Circular TourOA · Seen Nov 2021
  • House Robber IIOA · Seen Nov 2021
  • Clone an Undirected Graph StateOA · Seen Mar 2021
  • Longest Increasing Subsequence LengthOA · Seen Feb 2021
  • Maximum Later DifferenceOA · Seen Feb 2021
  • Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Card Game: Best First-Player ScoreShopify · OAShopify interview questions
  • Maximum Game Score with Fixed Forward JumpsStripe · OAStripe interview questions
  • 0/1 KnapsackConfluent · OAConfluent interview questions
  • Minimum Cost to Tile a Strip with Widths 1, 2, and 3Infosys · OAInfosys interview questions
  • Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
  • Maximum Subarray SumStraive · OA
  • Longest Increasing Price SubsequenceOpenAI · OAOpenAI interview questions
  • Lowest-Cost Shirt Color ScheduleElevenLabs · OAElevenLabs interview questions
  • Maximum Game Board ScoreJP Morgan Chase · OAJP Morgan Chase interview questions
  • API Bandwidth AllocationArista Networks · OAArista Networks interview questions

Interview reports that mention this question

  • Apple India SWE New Grad Interviews: 5 Experiences

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public int maxWeight(int[] weights, int w1, int w2) {
        // Write your solution here.
    }

}
Checking account…