Let [tex]d[/tex] be the distance between the two houses. Considering the formula
[tex]d=vt[/tex]
where [tex]d[/tex] is the distance, [tex]v[/tex] is the velocity and [tex]t[/tex] is the time, we can solve it for the time to get
[tex]t=\dfrac{d}{v}[/tex]
So, the two trips take the following time:
[tex]t_1 = \dfrac{d}{30},\quad t_2=\dfrac{d}{40}[/tex]
To get the average speed, we need to consider the two trips together. Globally, the person traveled a distance of [tex]2d[/tex], and the global time is the sum of the two times. We have
[tex]v=\dfrac{d_{TOT}}{t_{TOT}}=\dfrac{2d}{\frac{d}{30}+\frac{d}{40}}=\dfrac{2}{\frac{1}{30}+\frac{1}{40}}=\dfrac{2}{\frac{7}{120}}=\dfrac{240}{7}[/tex]