FastPrepFind a Subset of Exactly K Elements with a Target Sum
Problems/Find a Subset of Exactly K Elements with a Target Sum

Find a Subset of Exactly K Elements with a Target Sum

JP Morgan Chase logoJP Morgan Chase● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a JP Morgan Chase intern onsite interview. The author asks for a subset of length k that sums to n, and links a similar problem about a contiguous subarray of fixed length. Our version is about 75% the same as the literal task of subset, count and sum. We settled the conflict in the source by using a subset that need not be contiguous. We chose signed limits, edge rules, the interface and the examples. The linked page now shows a related window task, not the exact interview statement.

Given an integer array nums, return whether you can choose exactly k distinct array positions whose values sum to target. The chosen positions need not be adjacent. Each position may be used at most once; equal values at different positions remain separate choices.

For this exercise, assume signed values are allowed, the empty subset has sum 0, and the interface and bounds below apply.

Note that this asks for a subset of length k, not a contiguous subarray.

Function

hasKSubsetSum(nums: int[], k: int, target: int) → boolean

Examples

Example 1

nums = [1,50,2,50,3]k = 3target = 6return = true

Choose the distinct positions holding 1, 2 and 3. The elements need not be contiguous.

Example 2

nums = [1,2,3]k = 2target = 6return = false

No pair sums to 6. All three values sum to 6, but exactly two positions are required.

Constraints

  • For this exercise, assume 0 <= nums.length <= 20.
  • -100 <= nums[i] <= 100.
  • 0 <= k <= nums.length and -2000 <= target <= 2000.

More JP Morgan Chase Onsite Interview questions

  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU CacheONSITE INTERVIEW · Seen Nov 2026
  • Employees Above Average SalaryONSITE INTERVIEW · Seen Sep 2026
  • Highest-Earning Employees By DepartmentONSITE INTERVIEW · Seen Sep 2026
  • K Largest Integers in Descending OrderONSITE INTERVIEW · Seen Aug 2026
  • Tile a Three-Row Board with DominoesONSITE INTERVIEW · Seen Jan 2026
  • Two SumONSITE INTERVIEW · Seen Nov 2025
  • Intersection of Two ListsONSITE INTERVIEW · Seen Jan 2025
  • Maximum Subarray SumONSITE INTERVIEW · Seen Jan 2025
  • Fibonacci NumberONSITE INTERVIEW · Seen Nov 2024

Similar questions at other companies

  • Count Complete Tic-Tac-Toe GamesDecagon · PHONE SCREEN · ONSITE INTERVIEWDecagon interview questions
  • Enumerate Right-and-Down Matrix PathsOracle · ONSITE INTERVIEWOracle interview questions
  • Beautiful ArrangementMathWorks · ONSITE INTERVIEWMathWorks interview questions
  • First Valid Word SegmentationAmazon · ONSITE INTERVIEW
  • Generate One-Two Strings with Sum NGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Distinct Palindrome PartitionsMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
  • Unique Unordered Dice OutcomesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Count and Score Distinct Hat AssignmentsRoblox · ONSITE INTERVIEWRoblox interview questions
  • Partition a Card Deck into FifteensMeta · ONSITE INTERVIEWMeta interview questions
  • Maximum Currency Conversion Along a Simple PathRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions

Interview reports that mention this question

  • JPMorgan Chase Intern Campus Rounds: 4 Experiences (Part 2 of 5)

JP Morgan Chase interview experiences

  • JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
  • JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
  • JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
  • JP Morgan Chase Data and AI Intern Superday: 11 Experiences
  • JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)
All JP Morgan Chase interview questionsSee JP Morgan Chase hiring insights
Saved
class Solution {
    public boolean hasKSubsetSum(int[] nums, int k, int target) {
        // Write your code here.
    }
}
Checking account…