This is Exercise 3.12(b) of Gathmann's 2021 notes of Algebraic Geometry.
Let $Y$ be a non-empty irreducible subvariety of an affine variety $X$ and set $U=X\setminus Y$, Exercise 3.12(b) asks for an example that if $A(X)$ is not assumed to be a UFD, then there exists $Y$ such that $\text{codim}_XY\geq 2$ and $\mathscr{O}_X(U)\neq A(X)$, where $\mathscr{O}_X(U)$ is the ring of regular functions on $U$ and $A(X)$ is the coordinate ring of $X$.
I tried $X=V(x_1x_4-x_2x_3)\subset \mathbb{A}^4$, for which $A(X)$ is known as a non-UFD in a preceding exercise. The only (up to a permutation) irreducible subvariety of codimension $2$ that I find is $Y=V_X(x_1,x_2,x_3)$, since $V(x_1,x_2)$ is irreducible of codimension $1$. Now that
$$U = D(x_1)\cup D(x_2)\cup D(x_3),$$
hence elements in $\mathscr{O}_X(U)$ are the gluing of regular functions on $D(x_i)$'s, $i=1,2,3$. To find an element in $\mathscr{O}_X(U)$ that is not in $A(X)$, I considered $\frac{x_3}{x_1}\in \mathscr{O}_X(D(x_1))$ and $\frac{x_4}{x_2} \in \mathscr{O}_X(D(x_2))$. They do agree on $D(x_1)\cap D(x_2)$, however, I failed finding an extension of them to $D(x_3)$.
Similar difficulty appears when I tried considering $X=V(x_1x_5- x_2x_4, x_2x_6 -x_3x_5, x_1x_6-x_3x_4) $ and $X= V(x_1x_2x_3-x_4x_5x_6)$.
Is there any simple example for this question? Thanks in advance for any help.
EDIT: Also, the base field $K$ is always assumed to be algebraically closed, so the example better satisfies this condition...