Rider map geometry library
You build app/lib/eta.ts, a pure TypeScript library that takes geographic positions and travel-speed inputs and returns distances, ETA estimates, interpolated positions and compass bearings.
Ticket
Build a pure TypeScript geometry library in app/lib/eta.ts. Export LatLng and four functions: haversineKm, etaMinutes, interpolate and bearing.
The tests call these exact declarations:
export type LatLng = { lat: number; lng: number }
export function haversineKm(a: LatLng, b: LatLng): number
export function etaMinutes(distanceKm: number, speedKmh?: number): number | null
export function interpolate(from: LatLng, to: LatLng, t: number): LatLng
export function bearing(from: LatLng, to: LatLng): numberThe customer map already displays the rider's location. Your geometry helpers add an ETA and smoother marker movement without changing the existing tracking implementation. The rider's device reports positions through watchPositionAsync, while the customer receives tracking updates through the Reconnecting WebSocket flow.
Acceptance criteria
haversineKmreturns exactly0when given the same position twice.- The distance between
{ lat: 0, lng: 0 }and{ lat: 1, lng: 0 }is approximately111.19kilometres, within one decimal place. - Distance is symmetric: calculating the distance between
{ lat: 28.4595, lng: 77.0266 }and{ lat: 28.5355, lng: 77.391 }in either direction produces equal results to nine decimal places. The distance is greater than30km and less than45km. etaMinutes(5, 30)returns10, whileetaMinutes(5.1, 30)returns11.etaMinutes(10)returns30, using the default speed of20km/h.etaMinutes(0)andetaMinutes(0.001, 20)both return1.etaMinutes(5, 0)andetaMinutes(5, -3)both returnnull.- Interpolation between
{ lat: 10, lng: 20 }and{ lat: 12, lng: 24 }returns the exact starting position att = 0and the exact destination att = 1. - Interpolation halfway between
{ lat: 10, lng: 20 }and{ lat: 12, lng: 24 }returns{ lat: 11, lng: 22 }att = 0.5. - Interpolation clamps
tto the segment:t = -1returns the starting position, andt = 7returns the destination. - Bearing from
{ lat: 0, lng: 0 }points north at0degrees, east at90, south at180and west at270. - Bearing from
{ lat: 28.46, lng: 77.03 }to{ lat: 28.4, lng: 76.9 }is greater than or equal to0and less than360.
Constraints
- Implement all four exported functions in
app/lib/eta.ts. - Export
LatLngwith numericlatandlngproperties. - Use pure TypeScript. Do not import React or Expo.
- Use an Earth radius of exactly
6371kilometres in the haversine calculation. - Convert the distance to kilometres.
- Use a default ETA speed of
20km/h. - Round ETA up to whole minutes and enforce a minimum of
1minute for valid positive speeds. - Return
nullfor zero or negative speeds. - Interpolate latitude and longitude independently using a factor clamped between
0and1. - Return compass bearings in the range
0inclusive to360exclusive.
Design direction
Keep all four helpers together in app/lib/eta.ts. This module should not depend on application state, map components or network requests. Jest must be able to import it directly in Node.
Use the haversine formula for great-circle distance. Convert coordinates from degrees to radians before using trigonometric functions. A fixed Earth radius keeps the result consistent across calls.
For ETA, convert travel time from hours to minutes before rounding. Treat an invalid speed separately from a valid speed that produces less than one minute. These cases have different expected results.
Use linear interpolation independently for latitude and longitude. This exercise expects coordinate interpolation, not geographic route interpolation. For bearing, calculate the initial compass direction and normalize the result so negative angles wrap into the valid range.
Keep the helpers independent. Any implementation that meets the contract passes.
How to check your work
Run the backend test command from the repository root:
cd backend && make test W=5O3Then run the TypeScript compilation check:
cd app && npx tsc --noEmitRead the first failing assertion to identify which calculation or boundary condition differs from the expected value. Check the input coordinates, units, rounding and output range before changing the implementation.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Live order extensions
3 items
- 5.15Per-user Redis replay lognot completed
- 5.16Topic subscription registrynot completed
- 5.17Rider map geometry librarynot completed
Week 5 done · Up next · 5 modules
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
app/lib/eta.tsexportsLatLngand all four geometry helpers.haversineKmcalculates great-circle distance using an Earth radius of6371km.etaMinutesuses the default speed, rounds up and handles invalid speeds.interpolatereturns the expected endpoints and midpoint while clamping its factor.bearingreturns normalized compass directions in the range[0, 360).- Both
cd backend && make test W=5O3andcd app && npx tsc --noEmitpass.
Commands
cd backend && make test W=5O3cd app && npx tsc --noEmitTests
W5 O3 rider ETA and marker interpolation (12 tests)inapp-tests/lib/eta.test.ts