$f$ need not to be surjective because $g\circ f$ is defined even if the image of $f$ is not the whole domain of $g$.
Example. Let $A\subset B$ non empty, and $f:A\to B$ the inclusion, that is to say $f(x)=x$.
Define the map $g:B\to A$ as follows. Let $a_0\in A$ any point (which exists because $A\neq\emptyset$) then set:
$$g(x)=x\qquad\text{ for } x\in A$$
$$g(x)=a_0\qquad \text{ for } x\notin A$$
$g$ is a left inverse of $f$.
Your assertions about empty sets are incorrect.
Recall that a function from a set $X$ to a set $Y$ is a subset $f\subseteq X\times Y$ such that
- For all $x\in X$ there exists $y\in Y$ such that $(x,y)\in f$;
- If $(x,y),(x,y’)\in f$ for $x\in X$ and $y,y’\in Y$, then $y=y’$.
The function is injective if and only if for all $x,x’\in X$ and $y\in Y$, if $(x,y),(x’,y)\in f$, then $x=x’$.
The function is surjective if and only if for all $y\in Y$ there exists $x\in X$ such that $(x,y)\in f$.
If $X$ is empty, and $Y$ is any set, empty or not, the empty set $\varnothing\subseteq \varnothing\times Y = X\times Y$ satisfies the definition of being a function from $X=\varnothing$ to $Y$, hence is a function. Moreover, it satisfies the definition of being injective, hence is injective: there is always a function from the empty set to any set, and it is always injective.
However, that function only has a left inverse when the codomain is empty. So the statement you are trying to prove about injective functions is not quite true as written: you must exclude $X=\varnothing$ in order for it to be true.
That said, I applaud that you recognized the need to deal with the possibility of empty sets. Many people forget about that (for example, whoever wrote this problem up did!)
Your proof of (a) is correct; but you need to take into account the case where $X=\varnothing$. In this case, you can conclude from surjectivity that $Y=\varnothing$, and from the existence of a right inverse that $Y=\varnothing$.
As to “defining” the right inverse... In the nonempty sets dcadse, the “better way” of specifying the right inverse is to explicitly use the Axiom of Choice. For example, one formulation of the Axiom of Choice is that given any nonempty family of nonempty sets $\{A_i\}_{i\in I}$, there exists a function $g\colon I\to \cup A_i$ such that $g(i)\in A_i$ for each $i$. So you could take the family $\{A_y\}_{y\in Y}$, with $A_y=f^{-1}(\{y\})$, and just verify that a function $g\colon Y\to \cup A_y$ that you obtain from the Axiom of Choice is a right inverse for $f$. This is going to be the best you can do, at least for arbitrary sets.
Your example of non-uniqueness suffices; though in general you can show that the function has multiple right inverses if and only if it is not injective, that is not required.
For (b), the forward direction is fine (since we have assumed $X\neq\varnothing$. I would say the backward direction is fine, though I would note that $x_0$ exists because we are assuming that $X$ is not empty. Again, your example of nonuniqueness is fine. One can prove that the left inverse is unique if and only if $f$ is bijective, but you are not required to do so.
Best Answer
Let $f$ be injective. We construct a left-inverse for $f:A\to B$.
Let $a\in A\neq\emptyset$ be an arbitrary element.
Now take $g: B\to A$, $b\mapsto \begin{cases} f^{-1}(b),\text{if $b\in\mathrm{Im}f$}\\ a,\text{else}\end{cases}$.
Where $f^{-1}(b)$ notes the single element of $f^{-1}(\{b\})$, as justified further below.
This function is well-defined, as every element $b\in B$ has an image in $A$, and the image is unique, so $b$ does not get mapped onto several different elements in $A$. Here the assumption that $f$ is injective comes in, as this implies that every element in the image of $f$ has exactly one preimage. So $f^{-1}(b)$ (which is the set (not to confuse with a function) of all elements in $A$ with $f(a)=b$).
Now we have $g(f(a))=a$ for every $a\in A$, as $f(a)$ clearly is an element in $\mathrm{Im}f$ (image of $f$).
So $g$ is a left-inverse of $f$.
For the converse:
Let $g$ be a leftinverse of $f$. We have to show that $f$ is injective.
So let $f(a)=f(a')$. We have to show that $a=a'$.
We have $g(f(a))=a$, as $g$ is a leftinverse. Also we get $g(f(a'))=a'$ for the same reason.
But $a=g(f(a))\stackrel{f(a)=f(a')}{=}g(f(a')=a'$. So $f$ is indeed injective.