PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

Practice subset-sum interview variants, from an O(target) dynamic programming existence check to returning all matching subsets. The prompt emphasizes DP state, backtracking, duplicate values by index, exponential output size, and complexity analysis.

  • medium
  • Apple
  • Coding & Algorithms
  • Software Engineer

Solve Subset Sum And Return All Matching Subsets

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given an array of non-negative integers and a target, first determine whether any subset sums to the target using dynamic programming with `O(target)` space. Then implement a follow-up that returns all subsets whose values sum to the target. Function signature: ```python def subset_sum_exists(arr: list[int], target: int) -> bool: pass def all_subsets_with_sum(arr: list[int], target: int) -> list[list[int]]: pass ``` Constraints: - `arr` contains non-negative integers. - `target >= 0`. - For the existence function, use `O(target)` auxiliary space. - For the all-subsets function, exponential output size is expected. - Subsets should respect element positions; equal values at different indexes are distinct choices. Examples: ```text arr = [2, 3, 7, 8, 10], target = 11 subset_sum_exists returns True because 3 + 8 = 11. ``` ```text arr = [1, 2, 2], target = 3 all_subsets_with_sum may return [[1,2], [1,2]] for the two different 2 positions. ```

Quick Answer: Practice subset-sum interview variants, from an O(target) dynamic programming existence check to returning all matching subsets. The prompt emphasizes DP state, backtracking, duplicate values by index, exponential output size, and complexity analysis.

Return all subsets whose values sum to target. Equal values at different indexes are distinct choices. Return subsets sorted lexicographically for deterministic output.

Constraints

  • Non-negative integers are assumed.
  • Duplicate values at different indexes are distinct choices.

Examples

Input: ([2,3,7,8,10], 11)

Expected Output: [[3,8]]

Input: ([1,2,2], 3)

Expected Output: [[1,2],[1,2]]

Input: ([], 0)

Expected Output: [[]]

Input: ([], 5)

Expected Output: []

Input: ([0,0], 0)

Expected Output: [[],[0],[0],[0,0]]

Input: ([1,1,1], 2)

Expected Output: [[1,1],[1,1],[1,1]]

Input: ([5], 5)

Expected Output: [[5]]

Input: ([4,6,10], 3)

Expected Output: []

Hints

  1. For the boolean version, use DP.
  2. For all subsets, exponential output is unavoidable.
Last updated: Jul 8, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Minimal Unique Word Abbreviations - Apple (medium)
  • Convert a Roman Numeral to an Integer - Apple (medium)
  • Vertical Order Traversal of a Binary Tree - Apple (medium)
  • Implement a FIFO Queue Using Two Stacks - Apple (medium)
  • Minimum Cells to Bridge a Magic Grid - Apple (hard)