Practice Problems
very-frequent
This module works through three J1 and J2 problems from 2025 and 2024. Each one comes down to the same approach as the previous module: read carefully, work out what is being asked, and translate it directly into code.
J1 Walkthrough: Roller Coaster Ride2025 · J1 · Original problem · DMOJ
You are spending the day at the CEMC’s funfair. One of the rides at the funfair is a roller coaster which has one train with a number of cars. Each car holds the same number of people.
When you arrive at the roller coaster, you see that there is a line. Your job is to determine whether or not you will be on the next train ride, assuming that every car is fully occupied for every ride.

The first line of input contains a positive integer, , representing your place in line. For example, if is then you are the fifth person in line.
The second line contains a positive integer, , representing the number of cars the train has.
The third line contains a positive integer, , representing the number of people a single car holds.
Output either yes or no, indicating whether or not you will be on the next train ride.
14
3
2Output for Sample Input 1
no
Explanation of Output for Sample Input 1
The train has cars and each car holds people. Therefore, people can ride the next train. Since you are the 14th person in line, you will not be on the next train ride.
12
4
3Output for Sample Input 2
yes
Explanation of Output for Sample Input 2
The train has cars and each car holds people. Therefore, people can ride the next train. Since you are the 12th person in line, you will be on the next train ride.
Solution
We need to decide whether the person gets on the ride. The train holds people, and the position in line is , so the rider boards when:
Sample 2 checks the boundary: the train holds people and the person is 12th in line, so being exactly at the limit still counts.
Implementation
N = int(input())
C = int(input())
P = int(input())
if N <= C * P:
print("yes")
else:
print("no")J2 Walkthrough: Donut Shop2025 · J2 · Original problem · DMOJ
The owner of a donut shop spends the day baking and selling donuts.
Given the events that happen over the course of the day, your job is to determine the number of donuts remaining when the shop closes.

The first line of input contains a non-negative integer, , representing the number of donuts available when the shop first opens.
The second line contains a positive integer, , representing the number of events that happen over the course of the day. The next pairs of input lines describe these events.
The first line in the pair contains either the + (plus) symbol, indicating that donuts have been baked, or the - (minus) symbol, indicating that donuts have been sold. The second line in the pair contains a positive integer, , representing the quantity of donuts associated with the event.
For each sale of donuts, the value of will be less than or equal to the number of donuts available at that time.
Output the non-negative integer, , which is the number of donuts remaining when the shop closes.
10
3
+
24
-
6
-Output for Sample Input
16
Explanation of Output for Sample Input
The shop opened with donuts and there were events during the day. The owner first baked donuts. Then the owner sold donuts, followed by another . The number of donuts remaining is .
Solution
We keep a running total. Start with the donuts the shop opens with,
then process the events one at a time: a + adds donuts, a -
removes them. When the events run out, the total is the answer.
The input specification guarantees that every sale has enough donuts in stock, so the total never goes negative. There is nothing to check and nothing to clamp.
Implementation
D = int(input())
E = int(input())
for _ in range(E):
event = input()
Q = int(input())
if event == "+":
D += Q
else:
D -= Q
print(D)J2 Walkthrough: Dusa and the Yobis2024 · J2 · Original problem · DMOJ
Dusa eats Yobis, but only Yobis of a certain size.
If Dusa encounters a Yobi that is smaller than itself, it eats the Yobi, and absorbs its size. For example, if Dusa is of size and it encounters a Yobi of size , Dusa eats the Yobi and expands to size .
If Dusa encounters a Yobi that is the same size as itself or larger, Dusa runs away without eating the Yobi.
Dusa is currently facing a line of Yobis and will encounter them in order. Dusa is guaranteed to eventually encounter a Yobi that causes it to run away. Your job is to determine Dusa’s size when this happens.

The first line of input contains a positive integer, , representing Dusa’s starting size.
The remaining lines of input contain positive integers representing the sizes of the Yobis in order.
Output the positive integer, , which is Dusa’s size when it eventually runs away.
5
3
2
9
20
22
14Output for Sample Input 1
19
Explanation of Output for Sample Input 1
Dusa is large enough to eat the Yobi of size . This brings Dusa’s size to . Dusa is large enough to eat the Yobi of size . This brings Dusa’s size to . Dusa is large enough to eat the Yobi of size . This brings Dusa’s size to . The Yobi of size causes Dusa to run away.
10
10
3
5Output for Sample Input 2
10
Explanation of Output for Sample Input 2
The Yobi of size causes Dusa to run away, leaving its size unchanged.
Solution
This is the first problem where the input has no count — we are not told how many Yobis there are. We read sizes one at a time and react to each: if the Yobi is smaller than Dusa, Dusa eats it and grows; if it is the same size or bigger, Dusa runs away and the process ends.
The problem guarantees that a Yobi big enough to stop Dusa will eventually appear, so a loop that reads until it breaks is safe. That guarantee is also why there is no end-of-input handling to write.
Implementation
D = int(input())
while True:
size = int(input())
if size >= D:
break
D += size
print(D)Takeaways
None of these problems needs anything beyond input, a condition, and a loop. What they reward is careful reading: being exactly at capacity still gets the rider on the ride, the donut spec promises sales never exceed stock, and Dusa guarantees the loop terminates.
The next module covers problems where the input is text instead of numbers.