The problem: Now find a polynomial function $f$ of degree $n - 1$ such that $f(x_i) = a_i$, where $a, \ldots, a_n$ are given numbers.
I found that this question had been asked before, but I did not understand the solution.
Moving on to the actual problem: we are asked to use the formula derived in the previous problem, which is: $$f_i(x) = \prod^n_{j = 1, j \neq i}\frac{x - x_j}{x_i - x_j}.$$ This function is equal to $0$ for all $x_j$ if $j \neq i$, and equal to $1$ at $x_i$. Note that $x_j$ and $x_i$ come from the list of distinct numbers $x_1, \ldots, x_n$.
My thinking as to the solution is that we simply need to multiply $f_i(x)$ by $a_i$, giving us $$f(x) = a_i \cdot \prod^n_{j = 1, j \neq i}\frac{x - x_j}{x_i - x_j}.$$ Then when we plug in $x_i$ we would have $$f(x_i) = a_i \cdot \prod^n_{j = 1, j \neq i}\frac{x_i - x_j}{x_i - x_j} = a_i.$$
However, in the answer key, solution is $$f(x) = \sum^n_{i = 1} a_i \cdot \prod^n_{j = 1, j \neq i}\frac{x_i - x_j}{x_i - x_j}.$$ This seems like it must be false, as substituting in $x_i$ gives us $$f(x_i) = \sum^n_{i = 1} \cdot \prod^n_{j = 1, j \neq i}\frac{x_i - x_j}{x_i - x_j} = \sum^n_{i = 1} a_i.$$ It looks to me like $\sum^n_{i = 1} a_i$ only equals $a_i$ if $n = 1$. As a result, I'm very confused as to how this answer could be correct. Any clarification would be appreciated.