Find a function $f: X \rightarrow Y$ between metric spaces $X$ and $Y$ that is not continuous but has the property that for each closed ball $B$ of $Y, f^{-1}(B)$ is closed in $X$
Solution Attempt:
A continuous function $f : X \rightarrow Y$ is defined as : For every open set $V$ in $Y$, there's an open set $U \in X$ such that $f(U) \subseteq V$.
Strategy: The issue with closed sets while defining continuity can be that a closed set $U_o \in X $ can also contain limit points which may not get mapped to the closed set $V_o$.
EDIT: Let $f: X \rightarrow Y$ be a an identity function where $X$ is a non-discrete metric space and $Y$ is a metric space with the discrete metric. (As far as it's set elements were concerned, $X$=$Y$..)So, $f(x) = x ~\forall x \in X$. $X$ is non discrete metric space $\implies$ there is atleast one subset of $X$ which is closed but not open in $X$. Could you please explain how these arguments tell us that $f^{-1}(x_{|Y}) = x_{|X}$ is closed in $X$? . Thank you!
Let me answer the question you have asked as per your edit.
Let $f:(X,d)\to(X,\rho)$ be the identity map $f(x)=x$, where $\rho$ is the discrete metric and $d$ is any non-discrete metric.
Claim 1: The only closed balls in $(X,\rho)$ are singletons and the full space.
Proof: Let $x\in X$ and $r>0$. Let $\bar{B}_r(x)$ be the closed ball centered at $x$ of radius $r$. It is easy to see that if $r<1$ then $\bar{B}_r(x)=\{x\}$ and if $r\ge1$ then $\bar{B}_r(x)=X$. This proves the claim.
Claim 2: Inverse image of closed balls in $(X,\rho)$ are closed in $(X,d)$.
Proof: Clearly $f^{-1}(X)=X$ is closed in $(X,d)$. Next consider $f^{-1}(x)=\{x\}$. We will show that $\{x\}$ is closed in $(X,d)$ by showing that its complement $X\setminus\{x\}$ is open. Let $y\in X\setminus\{x\}$. Let $r=d(x,y)$. The open ball $B_{r/2}(y)$ does not contain $x$ and hence lies entirely in $X\setminus\{x\}$. Thus $X\setminus\{x\}$ is open which gives us $\{x\}$ is closed. This proves the claim.
Claim 3: The function $f$ is not continuous.
Proof: Since $(X,d)$ is not discrete, there is one subset of $X$ that is not open. We can assume without loss of generality that this set is a singleton say, $\{y\}$ (for otherwise every set would be open being the union of singletons). Let $V=\{y\}$, then $V$ is open in $(X,\rho)$. However, there is no non-empty open set $U$ in $(X,d)$ such that $f(U)\subset V=\{y\}$ (the only possibility for $U$ would have been $\{y\}$ which is not open). This proves the claim.
I hope this helps!