My interpretation is that by 'right answer' you don't mean whether you
computed the formula correctly, but whether correct answers from the formulas match
the median and mode of the sample or of the distribution from which the sample was drawn.
Sample. The median of a sample is the middle value of the sorted data
for samples of odd size and the average of the middle two values for
samples of even size. If you have the actual sample values (before binning)
that is easy enough to find. For a sample with tied observations the
mode is defined as the most frequently occurring value (if there is one). But for samples from a continuous distribution (where any ties occur only by rounding), I know of no 'official' definition of the
mode of a sample, but more on that later.
Here are two histograms of the same dataset of size $n=100$ with somewhat different choices of intervals. Accordingly, your formulas will give somewhat different answers. [From your formula, I got about 16 and about 15, respectively. You can use the counts atop histogram bars to check my answers.]
The true median of the 100 observations is 16.52 (rounded to 2 places).
The modal interval is $(10,20]$ in the top histogram and $(10,15]$ in the bottom histogram, so your formulas will be looking for the 'mode' of the
sample in two different intervals. [From your formula, I got roughly 15 for
the mode from both histograms.]
A histogram is one way to get a rough idea of the shape of a population density function, and your formula tries to give a value for the mode that matches
that density function. Usually, a somewhat more accurate method is to use a kernel density estimator (KDE). The figure below shows a the second histogram
of the data along with the KDE from R statistical software. By looking at the
KDE a little more closely, we can find that its maximum value is at about 15.3.
hist(x, prob=T, col="skyblue2")
lines(density(x, adj=1.5), col="red", lwd=2)
Population. If you know the CDF $F$ of the population, then the median
is the $x$-value where $F(x)=1/2.$ If you know the density function $f$ of
the population, then the mode is the $x$-value that maximizes $f(x),$ if
there is a unique maximum.
For example the distribution $\mathsf{Beta}(\text{shape}=\alpha=6, \text{rate}=\beta=1/3)$
has mode $(\alpha - 1)/\beta = 15$ and, by numerical integration,
the median is very nearly $17.01.$ [For more on beta distributions,
see Wikipedia.]
Of course, if the goal is to use a sample to estimate the median and mode of a population, a larger sample will tend to give better results than a smaller one. The figure below shows a histogram of a sample of size $n=2000$ from $\mathsf{Beta}(6,\,1/3),$ along with the population density function (black), and
the KDE of the sample (red).