AlgoMaster Logo

Area and Perimeter of Shapes

Last Updated: June 7, 2026

easy
3 min read

Understanding the Problem

This is a dispatch problem dressed up as geometry. Pick the right pair of formulas based on the shape string, plug in the numbers from dims, and round the results. The work is clean branching and exact arithmetic, not an algorithm.

Two details decide whether your output matches. The first is the value of PI. Different libraries carry different precision, so the problem fixes PI = 3.14159 to make circle results deterministic. The second is the triangle's square root. Instead of a built-in sqrt, we compute the root with the Babylonian method so behavior stays identical across every language. That method starts from a guess and repeatedly averages the guess with the number divided by the guess, converging on the root in a few steps.

Once those two pieces are settled, each shape reduces to two one-line formulas.

Key Constraints:

  • shape is one of four known strings. You can branch with a simple if-else chain or a switch. There is no need to handle unknown shapes.
  • Every number in dims is positive, so you never face a zero radius, a negative side, or a degenerate rectangle. The formulas apply directly without guarding against bad input.
  • For a triangle, the three sides form a valid triangle. The product s * (s - a) * (s - b) * (s - c) stays non-negative, so the square root is always defined.

Approach 1: Branch by Shape and Apply Formulas

Intuition

The shape string tells you which formula to use, so the solution mirrors the problem: one branch per shape, each holding the two formulas that define it. The rectangle and square are direct multiplication and addition. The circle uses the fixed PI, and the triangle uses the Babylonian square root.

After computing the raw area and perimeter, round both to two decimal places and return them together.

Algorithm

  1. Define PI = 3.14159 and a helper sqrt(x) that computes the square root with the Babylonian method.
  2. Read the shape and branch:
    • For "rectangle", set length = dims[0], width = dims[1]. Area is length * width, perimeter is 2 * (length + width).
    • For "square", set side = dims[0]. Area is side * side, perimeter is 4 * side.
    • For "circle", set r = dims[0]. Area is PI * r * r, perimeter is 2 * PI * r.
    • For "triangle", set a, b, c = dims[0], dims[1], dims[2]. Perimeter is a + b + c. Compute s = (a + b + c) / 2, then area is sqrt(s * (s - a) * (s - b) * (s - c)).
  3. Round both the area and the perimeter to two decimal places.
  4. Return [area, perimeter].

Example Walkthrough

Take the rectangle dims = [4, 3]:

0
4
1
3
dims

The shape is "rectangle", so length = 4 and width = 3. Area is 4 * 3 = 12, and perimeter is 2 * (4 + 3) = 14. Rounded, the result is:

0
12
1
14
result

Now take the 3-4-5 triangle dims = [3, 4, 5]:

0
3
1
4
2
5
dims

The perimeter is 3 + 4 + 5 = 12. For the area, s = 12 / 2 = 6, so the product under the root is 6 * (6 - 3) * (6 - 4) * (6 - 5) = 6 * 3 * 2 * 1 = 36. The square root of 36 is 6, so the area is 6. Rounded, the result is:

0
6
1
12
result

Code