PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates algorithmic problem-solving skills in combinatorics and resource-constrained subset-sum computation, focusing on dynamic programming, optimization, and complexity analysis within the Coding & Algorithms domain.

  • Medium
  • Visa
  • Coding & Algorithms
  • Software Engineer

Compute distinct sums from limited coins

Company: Visa

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

You are given two integer arrays, denom[0..n-1] and count[0..n-1], where denom[i] > 0 is a coin denomination and count[i] >= 0 is the number of available coins of that denomination. Return the number of distinct positive sums that can be formed using at most count[i] coins of value denom[i] (each coin is identical and can be used at most once). Describe your algorithm and analyze its time and space complexity. Discuss how you would handle large maximum reachable sums (e.g., up to 1e 6).

Quick Answer: This question evaluates algorithmic problem-solving skills in combinatorics and resource-constrained subset-sum computation, focusing on dynamic programming, optimization, and complexity analysis within the Coding & Algorithms domain.

You are given two integer arrays, `denom[0..n-1]` and `count[0..n-1]`, where `denom[i] > 0` is a coin denomination and `count[i] >= 0` is the number of available coins of that denomination. Coins of the same denomination are identical, and each coin may be used at most once (so you can use anywhere from 0 to `count[i]` coins of value `denom[i]`). Return the number of **distinct positive sums** that can be formed. The empty selection produces a sum of 0 and is not counted. Example: `denom = [1, 5]`, `count = [2, 1]`. You have two 1-coins and one 5-coin. The achievable positive sums are {1, 2, 5, 6, 7}, so the answer is 5. Follow-up to discuss aloud: how would you bound time and space when the maximum reachable sum is large (e.g. up to 1e6)? A boolean DP / bitset over reachable sums of size `S = sum(denom[i] * count[i])` runs in O(total_coins * S) naively, or O(n * S) with a bounded-knapsack trick (sliding window per denomination); a bitset makes each denomination's update O(S / 64) per shift.

Constraints

  • 1 <= n, but n may be 0 (empty arrays) -> answer 0
  • denom[i] > 0
  • count[i] >= 0 (a count of 0 contributes nothing)
  • Coins of the same denomination are identical and order-independent
  • Maximum reachable sum S can be large (e.g. up to 1e6); prefer a boolean array / bitset over a hash set at that scale

Examples

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

Expected Output: 5

Explanation: Two 1-coins and one 5-coin. Positive sums: {1,2,5,6,7} -> 5 distinct.

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

Expected Output: 3

Explanation: One 2 and one 3. Positive sums: {2,3,5} -> 3 distinct.

Input: ([1], [4])

Expected Output: 4

Explanation: Four 1-coins give sums {1,2,3,4} -> 4 distinct.

Input: ([], [])

Expected Output: 0

Explanation: No coins -> no positive sums.

Input: ([5], [0])

Expected Output: 0

Explanation: A denomination with count 0 contributes nothing.

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

Expected Output: 2

Explanation: Duplicate denomination: effectively two 3-coins -> sums {3,6} -> 2 distinct.

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

Expected Output: 12

Explanation: Three 1s, two 2s, one 5. Reachable positive sums are {1,2,3,4,5,6,7,8,9,10,11,12} -> 12 distinct.

Input: ([7], [3])

Expected Output: 3

Explanation: Three 7-coins give {7,14,21} -> 3 distinct.

Hints

  1. Model it as bounded subset-sum: maintain the set of all sums reachable so far, starting from {0}.
  2. For each denomination d with c copies, a previously reachable sum b lets you newly reach b+d, b+2d, ..., b+c*d.
  3. Process denominations one at a time so each coin of a given denomination is only added once per base — never re-feed sums created within the same denomination's expansion, or you'll exceed its count.
  4. At scale (S up to 1e6), replace the hash set with a boolean array of size S+1 (or a 64-bit bitset) and use the classic bounded-knapsack sliding-window trick to keep it O(n*S).
  5. Subtract 1 (drop the sum 0) at the end since only positive sums count.
Last updated: Jun 26, 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
  • 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

  • Solve Two Algorithm Challenges - Visa (hard)
  • Solve Three Array Coding Problems - Visa (medium)
  • Maintain pair-sum counts under replacements - Visa (Medium)
  • Design rotating warehouse simulator with closures - Visa (Medium)