Fewest Coins for an Amount
Updated
Problem statement
Pay exactly amount using coins whose values appear in coins. A value can be used again and again. Return how few coins that takes, or -1 when amount cannot be paid exactly. Paying 0 takes 0 coins.
Function
coinChange(coins: int[], amount: int) → intExamples
Example 1
coins = [3,7,10]amount = 21return = 3Three 7 coins make 21. Seven 3 coins also make 21, but that uses more coins, so the answer is 3. Grabbing 10 first does not help: 10 + 10 leaves 1, and a single 10 leaves 11, which no mix of 3s and 7s can pay.
Example 2
coins = [5,8]amount = 11return = -1Try the number of 8 coins. With one, 3 is left, which is smaller than 5. With none, only multiples of 5 can be paid, and 11 is not one. Two 8s already pass 11. No mix works, so the answer is -1.
Example 3
coins = [9,20]amount = 47return = 447 = 20 + 9 + 9 + 9 uses four coins. Two 20s leave 7, which no 9 can cover, and 9s alone cannot reach 47 because it is not a multiple of 9.
Example 4
coins = [6,13]amount = 0return = 0There is nothing to pay, so no coins are needed and the answer is 0, not -1.
Constraints
1 <= coins.length <= 121 <= coins[i] <= 2^31 - 1- All values in
coinsare different. 0 <= amount <= 10^4
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- Coin ChangeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Coin ChangeKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Coin ChangePinterest · ONSITE INTERVIEWPinterest interview questions
- Coin ChangeBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Choose a Shortest Path That Avoids BobGoogle · ONSITE INTERVIEWGoogle interview questions
- Grid Robot Route with Charging PrioritiesUber · PHONE SCREEN · ONSITE INTERVIEWUber interview questions
- Coin ChangeThe Walt Disney Company · OA · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Shortest Path Visiting All NodesInMobi · ONSITE INTERVIEWInMobi interview questions
- Minimum Distance Matching in a ForestScale AI · PHONE SCREEN · ONSITE INTERVIEWScale AI interview questions
- Path Through an O/X Grid Using Only Right and DownAmazon · PHONE SCREEN
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences