Challenge: The Coin Change Problem
Let's solve a coding challenge on the different ways to represent a given number of cents.
We'll cover the following
Problem statement
Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents), and pennies (1 cent), write some code to calculate the number of ways to represent n
cents.
Input
The values of the coins available to represent the cents with (the denominations), the number of denominations, and the number of cents
Output
The number of ways that the given number of cents can be represented
Sample input
amount = 10 # num of cents
denoms = [25, 10, 5, 1]
denomsLength = 4
Sample output
result = 4 # num of ways to make change for 10 cents
The change for 10
cents can be made in the following 4
ways:
-
{
1,1,1,1,1,1,1,1,1,1
} -
{
5,1,1,1,1,1
} -
{
5,5
} -
{
10
}
Coding challenge
First, take a close look at this problem and design a step-by-step algorithm before jumping to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided in the solution section. Good luck!
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.