Strava: see where your effort differs on the same stretch of road
Strava helps runners and cyclists record activities and review their performance. Segment comparisons focus on the same stretch of a route, making it possible to revisit personal efforts with an explicit baseline.
Break down the problem and try your own design and implementation first. Then explore the reasoning to check states, edge cases, trade-offs, and ways to test your approach. The guide offers a reasoning path, not a complete implementation.
Put effort units and comparison rules in one module
Suppose you are building a sports app’s comparison card. Records may use seconds or milliseconds. The card needs one interface to decide whether two efforts can be compared and whether the current one is faster or slower than the baseline.
- Inputs: Implement compareEfforts(current, baseline). Each record has segmentId (the segment’s identifier), sport (run or ride), duration (time taken), and unit (s for seconds or ms for milliseconds). duration is guaranteed to be a positive finite number, and all other fields have these stated types.
- Rules: Only the same segment and sport are comparable. Otherwise return {ok:false,reason:"not-comparable"}. For comparable records, convert times to milliseconds and return {ok:true,deltaMs,relation}. deltaMs is current time minus baseline time. Negative means faster, zero means same, and positive means slower. Do not modify the inputs.
- Check: For the same segment and sport, current 60s versus baseline 62000ms gives −2000 and faster; 60000ms versus 60s gives 0 and same; 63s versus 62s gives 1000 and slower. A different segment or a change from run to ride returns not-comparable.
- Deliver: Submit the small module, an explanation of its results, and local example checks. Verify that inputs remain unchanged. Do not query leaderboards or draw charts.
Explore the reasoning
-
Decide which records are comparable. Give an invalid comparison a different result from two equal times.
-
Normalize units at the module boundary and explain the difference’s sign, so each page does not have to guess the units.
-
Check the interface with mixed units, equal values and different comparison subjects, then confirm that calling it leaves the inputs unchanged.