Back to Practice
#0207
Average Speed for a Round Trip
MediumAptitude25 min20 XP
Problem
A person travels from A to B at 30 km/h and returns from B to A at 60 km/h. Find the average speed for the whole trip.
Why This Matters
Average-speed traps are common because equal distances and equal times use different formulas.
Examples
Example 1
InputGoing speed = 30 km/h, return speed = 60 km/h, same distance each way
Output40 km/h
For equal distances, average speed is 2ab / (a + b) = 2*30*60/90 = 40.
Constraints
- Return the exact requested result.
- Handle common edge cases.
- Keep the solution readable before optimizing.
CodeReasoning
A person travels from A to B at 30 km/h and returns from B to A at 60 km/h. Find the average speed for the whole trip.
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
InputGoing speed = 30 km/h, return speed = 60 km/h, same distance each way
Expected40 km/h
For equal distances, average speed is 2ab / (a + b) = 2*30*60/90 = 40.
Hidden Test Categories
empty or minimal inputrepeated valueslarger realistic input