Top K Subset Sums of Nonnegative Weights
Updated
Problem statement
You are given distinct objects whose non-negative integer weights are stored in weights, and an integer k.
Every subset of objects contributes one subset sum, including the empty subset. Different subsets are counted separately even when their sums are equal.
Return the largest k subset sums, from largest to smallest.
Function
topKSubsetSums(weights: int[], k: int) → long[]Examples
Example 1
weights = [1,2,4]k = 5return = [7,6,5,4,3]The five largest sums among all eight subsets are 7, 6, 5, 4, 3.
Example 2
weights = [2,3]k = 4return = [5,3,2,0]All four subset sums are returned.
Example 3
weights = [0,5]k = 4return = [5,5,0,0]The zero-weight object creates different subsets with equal sums, and each subset is retained.
Constraints
1 <= weights.length <= 40.0 <= weights[i] <= 1000000000.1 <= k <= min(100000, 2^weights.length).
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- IPOTekion · PHONE SCREENTekion interview questions
- Smallest Range Covering Elements from K ListsSnap Inc. · PHONE SCREENSnap Inc. interview questions
- IPOInMobi · PHONE SCREENInMobi interview questions
- Meeting Rooms IIIBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Running Median of a StreamPeopleStrong · PHONE SCREEN · ONSITE INTERVIEWPeopleStrong interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
- Meeting Rooms IIeBay · PHONE SCREENeBay interview questions