Code Logo

Check Liquid Balance Scale

Published at19 Apr 2026
Medium 0 views
Like0

This challenge is based on a real-world weighing scenario. A digital balance scale is used to compare the mass of liquid stored in two cylindrical containers: one on the left side and one on the right side.

Both containers have the same radius r, but they may contain different liquid heights and different liquid densities. You are given the radius of the container, the height of liquid on the left, the density of the left liquid, the height of liquid on the right, and the density of the right liquid.

To keep the arithmetic simple, use the simplified value pi = 3. The volume of a cylinder is:

volume = 3 * r * r * t

The mass of the liquid is:

mass = volume * density

Your program must calculate the mass on both sides. If the two masses are equal, print BALANCE. Otherwise, print the absolute difference between the two masses.

This problem is designed to practice modular thinking. A clean solution separates the work into a function for volume, a function for mass, and a procedure to display the final scale result.

Example Input & Output

Example 1
Input
r = 2, t_left = 5, d_left = 2, t_right = 4, d_right = 3
Output
24
Explanation

The left mass is 120 and the right mass is 144, so the scale difference is 24.

Example 2
Input
r = 1, t_left = 2, d_left = 3, t_right = 3, d_right = 2
Output
BALANCE
Explanation

Both sides have mass 18, so the scale is balanced.

Example 3
Input
r = 3, t_left = 1, d_left = 5, t_right = 2, d_right = 2
Output
27
Explanation

The left mass is 135 and the right mass is 108, so the difference is 27.

Algorithm Flow

Recommendation Algorithm Flow for Check Liquid Balance Scale
Recommendation Algorithm Flow for Check Liquid Balance Scale

Solution Approach

A good way to solve this problem is to break it into three smaller tasks.

First, create a function to compute the cylinder volume. Since the simplified rule in this problem is pi = 3, the formula becomes:

volume <- 3 * r * r * t

Second, create another function to compute the liquid mass. That function can call the volume function, then multiply the result by the density:

mass <- volume * density

Third, create a procedure that compares the left mass and the right mass. If they are equal, output BALANCE. If they are different, output the absolute difference. Because there is no built-in absolute value requirement here, you can use a simple condition:

if m1 = m2 then output("BALANCE") else if m1 > m2 then output(m1 - m2) else output(m2 - m1) endif

In the main program, read all inputs, compute the left and right masses with the helper functions, then call the display procedure. The overall running time is O(1) because the program only performs a fixed number of calculations and comparisons.

Best Answers

Solutions Coming Soon

Verified best solutions for this Challenge are still being analyzed and will be available soon.