Unicorns / VO Interview Question: Area Under a Piecewise Line Graph | Prefix Sum, Math, Line Area

18 Views
No Comments

You are given a graph with X and Y axes. Each axis represents positive integers.

We will plot a graph based on given X and Y points.

Starting from (0,0), X will increment by 1 on the X axis.

The Y axis will represent the following format:

  • If X is 0, Y is 0. (0,0)
  • If X is 1, Y is 1. (1,1)
  • If X is 2 or more, the X-th Y is (X-1-th Y + X-2-th Y)

For example, if X = 2, Y = (0 + 1) = 1.

Given a series of (X, Y) points, a line will be drawn.

See the example image for visualization.

Write a function that takes input value X, and calculate the area underneath the drawn lines.

This problem asks you to generate a sequence of points from a Fibonacci-like recurrence, connect them with straight line segments, and compute the total area under the resulting polyline. The key is to treat the graph as a sum of trapezoids between consecutive points, which makes the solution straightforward once the Y-values are built. A careful linear pass is usually enough, and the main challenge is translating the recurrence and the geometry into a clean formula.

END
 0