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:

ts
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): number

The 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

  • haversineKm returns exactly 0 when given the same position twice.
  • The distance between { lat: 0, lng: 0 } and { lat: 1, lng: 0 } is approximately 111.19 kilometres, 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 than 30 km and less than 45 km.
  • etaMinutes(5, 30) returns 10, while etaMinutes(5.1, 30) returns 11.
  • etaMinutes(10) returns 30, using the default speed of 20 km/h.
  • etaMinutes(0) and etaMinutes(0.001, 20) both return 1.
  • etaMinutes(5, 0) and etaMinutes(5, -3) both return null.
  • Interpolation between { lat: 10, lng: 20 } and { lat: 12, lng: 24 } returns the exact starting position at t = 0 and the exact destination at t = 1.
  • Interpolation halfway between { lat: 10, lng: 20 } and { lat: 12, lng: 24 } returns { lat: 11, lng: 22 } at t = 0.5.
  • Interpolation clamps t to the segment: t = -1 returns the starting position, and t = 7 returns the destination.
  • Bearing from { lat: 0, lng: 0 } points north at 0 degrees, east at 90, south at 180 and west at 270.
  • Bearing from { lat: 28.46, lng: 77.03 } to { lat: 28.4, lng: 76.9 } is greater than or equal to 0 and less than 360.

Constraints

  • Implement all four exported functions in app/lib/eta.ts.
  • Export LatLng with numeric lat and lng properties.
  • Use pure TypeScript. Do not import React or Expo.
  • Use an Earth radius of exactly 6371 kilometres in the haversine calculation.
  • Convert the distance to kilometres.
  • Use a default ETA speed of 20 km/h.
  • Round ETA up to whole minutes and enforce a minimum of 1 minute for valid positive speeds.
  • Return null for zero or negative speeds.
  • Interpolate latitude and longitude independently using a factor clamped between 0 and 1.
  • Return compass bearings in the range 0 inclusive to 360 exclusive.

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:

bash
cd backend && make test W=5O3

Then run the TypeScript compilation check:

bash
cd app && npx tsc --noEmit

Read 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.

Finished Exercise 5.17?

Live order extensions

3 items

  • 5.15Per-user Redis replay lognot completed
  • 5.16Topic subscription registrynot completed
  • 5.17Rider map geometry librarynot completed

Week 6 · Performance, caching and load testing

Week 5 done · Up next · 5 modules

Open week 6

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. app/lib/eta.ts exports LatLng and all four geometry helpers.
  2. haversineKm calculates great-circle distance using an Earth radius of 6371 km.
  3. etaMinutes uses the default speed, rounds up and handles invalid speeds.
  4. interpolate returns the expected endpoints and midpoint while clamping its factor.
  5. bearing returns normalized compass directions in the range [0, 360).
  6. Both cd backend && make test W=5O3 and cd app && npx tsc --noEmit pass.

Commands

terminal
cd backend && make test W=5O3
terminal
cd app && npx tsc --noEmit

Tests

  • W5 O3 rider ETA and marker interpolation (12 tests) in app-tests/lib/eta.test.ts