PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's grasp of dynamic programming, specifically the unbounded knapsack (coin change) pattern for finding the minimum number of items from an unlimited supply that sum to an exact target. It tests the ability to define subproblems, handle infeasible cases, and reconstruct an optimal combination, making it a common way to assess algorithmic problem-solving in coding interviews.

  • medium
  • Airbnb
  • Coding & Algorithms
  • Machine Learning Engineer

Minimum Canisters to Top Up an Exact Target

Company: Airbnb

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Minimum Canisters to Top Up an Exact Target During a layover, the ground crew must top up an auxiliary tank by **exactly** `target` units of coolant before the next leg can depart. The depot stocks coolant in sealed canisters of several fixed capacities. Each capacity is available in **unlimited** supply, and a canister must be emptied **completely** into the tank (you cannot use a partial canister). Given the target amount and the list of available canister capacities, return the **minimum number of canisters** whose capacities sum to exactly `target`. If no combination of canisters can sum to exactly `target`, return `-1`. ## Input / Output - **Input:** - `target`: a non-negative integer — the exact amount that must be added. - `capacities`: a list of distinct positive integers — the canister sizes the depot stocks. Each size may be used any number of times (including zero). - **Output:** an integer — the minimum number of canisters that sum to exactly `target`, or `-1` if it is impossible. Suggested signature: ```python def min_canisters(target: int, capacities: list[int]) -> int: ... ``` ## Constraints - `0 <= target <= 10_000` - `1 <= len(capacities) <= 100` - `1 <= capacities[i] <= 10_000` - All values in `capacities` are distinct positive integers. ## Examples **Example 1** ``` target = 11 capacities = [1, 2, 5] => 3 ``` Explanation: `5 + 5 + 1 = 11` uses 3 canisters, which is optimal. No 2-canister combination sums to 11. **Example 2** ``` target = 3 capacities = [2] => -1 ``` Explanation: Only size-2 canisters exist; no number of them sums to exactly 3. **Example 3** ``` target = 0 capacities = [1, 2, 5] => 0 ``` Explanation: Nothing needs to be added, so zero canisters are required. **Example 4** ``` target = 100 capacities = [1, 5, 10, 25] => 4 ``` Explanation: `25 * 4 = 100` uses 4 canisters, which is optimal. ## Edge Cases to Handle - `target == 0` should return `0`. - A `target` that cannot be formed by any combination must return `-1` (e.g., all capacities larger than `target`, or a parity/divisibility mismatch). - Large `target` with small capacities (the answer can be as large as `target` when a size-1 canister exists). ## Follow-up Beyond the minimum **count**, also reconstruct **one** concrete combination that achieves it: return the multiset (or sorted list) of canister capacities used. For Example 1 this would be `[1, 5, 5]`. Any combination that uses the minimum number of canisters and sums exactly to `target` is acceptable.

Quick Answer: This question evaluates a candidate's grasp of dynamic programming, specifically the unbounded knapsack (coin change) pattern for finding the minimum number of items from an unlimited supply that sum to an exact target. It tests the ability to define subproblems, handle infeasible cases, and reconstruct an optimal combination, making it a common way to assess algorithmic problem-solving in coding interviews.

During a layover, the ground crew must top up an auxiliary tank by **exactly** `target` units of coolant. The depot stocks coolant in sealed canisters of several fixed capacities. Each capacity is available in **unlimited** supply, and a canister must be emptied **completely** into the tank (no partial canisters). Given the target amount and the list of available canister capacities, return the **minimum number of canisters** whose capacities sum to exactly `target`. If no combination sums to exactly `target`, return `-1`. ### Input / Output - `target`: a non-negative integer — the exact amount that must be added. - `capacities`: a list of distinct positive integers — the canister sizes stocked. Each size may be used any number of times (including zero). - Return an integer: the minimum number of canisters that sum to exactly `target`, or `-1` if impossible. ### Constraints - `0 <= target <= 10_000` - `1 <= len(capacities) <= 100` - `1 <= capacities[i] <= 10_000`, all distinct. ### Examples - `target = 11`, `capacities = [1, 2, 5]` => `3` (5 + 5 + 1) - `target = 3`, `capacities = [2]` => `-1` - `target = 0`, `capacities = [1, 2, 5]` => `0` - `target = 100`, `capacities = [1, 5, 10, 25]` => `4` (25 * 4) ### Note This is the classic unbounded coin-change *minimum count* problem: capacities are coin denominations and `target` is the amount. A greedy 'take the largest that fits' strategy does **not** always work (e.g. `target = 6`, `capacities = [1, 3, 4]` needs `3 + 3`, not `4 + 1 + 1`), so use dynamic programming.

Constraints

  • 0 <= target <= 10_000
  • 1 <= len(capacities) <= 100
  • 1 <= capacities[i] <= 10_000
  • All capacities are distinct positive integers

Examples

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

Expected Output: 3

Explanation: 5 + 5 + 1 = 11 uses 3 canisters; no 2-canister combination reaches 11.

Input: (3, [2])

Expected Output: -1

Explanation: Only size-2 canisters exist; any sum of 2s is even, so 3 is unreachable.

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

Expected Output: 0

Explanation: Nothing needs to be added, so zero canisters are required.

Input: (100, [1, 5, 10, 25])

Expected Output: 4

Explanation: 25 * 4 = 100 uses 4 canisters, which is optimal.

Input: (7, [3, 5])

Expected Output: -1

Explanation: No combination of 3s and 5s sums to exactly 7.

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

Expected Output: 2

Explanation: 3 + 3 = 6 uses 2 canisters. A greedy 4 + 1 + 1 would use 3, so greedy is suboptimal here.

Input: (9999, [10000])

Expected Output: -1

Explanation: The only canister is larger than the target, so the target can never be reached.

Input: (23, [7, 5, 1])

Expected Output: 5

Explanation: 7 + 7 + 7 + 1 + 1 = 23 uses 5 canisters; no 4-canister combination sums to 23.

Hints

  1. Think of capacities as coin denominations and target as the amount: this is the minimum-coins version of the coin-change problem.
  2. Let dp[a] be the fewest canisters that sum to exactly a. Base case dp[0] = 0; every other dp[a] starts at infinity (unreachable).
  3. Transition: dp[a] = 1 + min(dp[a - c]) over all capacities c <= a. Build dp from 1 up to target.
  4. If dp[target] is still infinity at the end, the target is impossible, so return -1. Greedy (largest-first) fails on inputs like target=6, capacities=[1,3,4].
Last updated: Jul 1, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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

  • Count Candies from Unlockable Boxes - Airbnb (medium)
  • Find Optimal Property Combination - Airbnb (medium)
  • Determine Exact Layover Booking - Airbnb (medium)
  • Solve Linked-List and Iterator Problems - Airbnb
  • Implement Text Layout and Query Parsing - Airbnb (easy)