Skip to content

Shape Area

Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.

A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.

Image Credit: CodeSignal

Example

  • For n = 2, the output should be

    shape_area(n) = 5
  • For n = 3, the output should be

    shape_area(n) = 13

Solution

py
def shape_area(n):
    return (n**2 + (n-1)**2)


print(shape_area(3))
js
function shapeArea(n) {
  return n ** 2 + (n - 1) ** 2;
}

console.log(shapeArea(3));

my thoughts are neither my employer's nor my wife's