Implement a vectorized haversine, Manhattan distance and bearing transformer for trip coordinates

Quick Overview

Implement a vectorized pandas and NumPy transformer that turns pickup and dropoff coordinates into haversine distance, a Manhattan-style distance and the initial bearing. It tests spherical trigonometry, numerical edge cases such as antipodal points and antimeridian crossings, row-level NaN handling and input validation.

Implement a vectorized haversine, Manhattan distance and bearing transformer for trip coordinates

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `GeoTripFeatures.transform(X)`, which takes a DataFrame with `pickup_latitude`, `pickup_longitude`, `dropoff_latitude` and `dropoff_longitude` (degrees). It returns a new DataFrame with columns `haversine_km`, `manhattan_km` and `bearing_deg` in that order. Use vectorized NumPy only, with no row loops and no spatial libraries. Requirements: - Distances use haversine with `earth_radius_km`. `manhattan_km` is the haversine distance along the pickup latitude (pickup lon → dropoff lon) plus the haversine distance along the pickup longitude (pickup lat → dropoff lat). - `bearing_deg` is the initial great-circle bearing in `[0, 360)` (0 = north, 90 = east). Identical points give all zeros. Antipodal points and antimeridian crossings must not produce `NaN`. - Output is `float64` and keeps `X.index`. Extra columns are ignored, and `X` must not be mutated. - A `NaN` coordinate makes only that row's outputs `NaN`. - A missing required column or a non-NaN `|latitude| > 90` raises `ValueError`. A non-DataFrame input raises `TypeError`. Example: ```python X = pd.DataFrame([[0, 0, 0, 1], [0, 0, 1, 0]], columns=["pickup_latitude", "pickup_longitude", "dropoff_latitude", "dropoff_longitude"]) GeoTripFeatures().fit_transform(X) # haversine_km manhattan_km bearing_deg # 0 111.194927 111.194927 90.0 # 1 111.194927 111.194927 0.0 ``` ```hint Antipodal points For two points on opposite sides of the globe, rounding can push the haversine term a little past its mathematical limit. Check what the inverse sine does with such a value. ``` ```hint Bearing range The two-argument arctangent returns angles between -180 and 180 degrees. Work out how to map that onto [0, 360) and check the values that land on the boundary. ``` ### Constraints & Assumptions - `X` is a pandas DataFrame with `n` rows; coordinates are in degrees and may contain NaN. - Longitudes can sit on either side of the antimeridian, so a trip from 179 to -179 is a short hop. - `earth_radius_km` is a constructor argument; the example uses the default of 6371.0. ### Clarifying Questions to Ask - Should longitudes outside [-180, 180] be rejected, or accepted as they are? - Is the transformer stateless, so that `transform` works without a prior call to `fit`? - For the east-west leg, is the haversine distance between the two points at the pickup latitude what you want, or the arc length along that parallel? ### What a Strong Answer Covers - A fully vectorized haversine with its argument kept in range so antipodal rows stay finite. - The Manhattan decomposition with each leg measured exactly as specified: east-west at the pickup latitude, north-south at the pickup longitude. - The initial bearing from the two-argument arctangent, normalized into [0, 360), with identical points and antimeridian crossings handled. - Row-local NaN propagation and the required validation: `TypeError`, missing columns, and the latitude range check that skips NaN. - A new float64 frame on the input index, in the required column order, with `X` left untouched. ### Follow-up Questions - How would you make this a drop-in scikit-learn transformer? - Why can a city grid's Manhattan distance differ from this lat/lon decomposition? - How would you feed bearing to a model given its circular nature?

Overview: Implement a vectorized pandas and NumPy transformer that turns pickup and dropoff coordinates into haversine distance, a Manhattan-style distance and the initial bearing. It tests spherical trigonometry, numerical edge cases such as antipodal points and antimeridian crossings, row-level NaN handling and input validation.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement GeoTripFeatures.transform(X), which takes a DataFrame with pickup_latitude, pickup_longitude, dropoff_latitude and dropoff_longitude (degrees). It returns a new DataFrame with columns haversine_km, manhattan_km and bearing_deg in that order. Use vectorized NumPy only, with no row loops and no spatial libraries.

Requirements:

  • Distances use haversine with earth_radius_km . manhattan_km is the haversine distance along the pickup latitude (pickup lon → dropoff lon) plus the haversine distance along the pickup longitude (pickup lat → dropoff lat).
  • bearing_deg is the initial great-circle bearing in [0, 360) (0 = north, 90 = east). Identical points give all zeros. Antipodal points and antimeridian crossings must not produce NaN .
  • Output is float64 and keeps X.index . Extra columns are ignored, and X must not be mutated.
  • A NaN coordinate makes only that row's outputs NaN .
  • A missing required column or a non-NaN |latitude| > 90 raises ValueError . A non-DataFrame input raises TypeError .

Example:

X = pd.DataFrame([[0, 0, 0, 1], [0, 0, 1, 0]], columns=["pickup_latitude", "pickup_longitude", "dropoff_latitude", "dropoff_longitude"])
GeoTripFeatures().fit_transform(X)
#    haversine_km  manhattan_km  bearing_deg
# 0    111.194927    111.194927         90.0
# 1    111.194927    111.194927          0.0

Constraints & Assumptions

  • X is a pandas DataFrame with n rows; coordinates are in degrees and may contain NaN.
  • Longitudes can sit on either side of the antimeridian, so a trip from 179 to -179 is a short hop.
  • earth_radius_km is a constructor argument; the example uses the default of 6371.0.

Clarifying Questions to Ask Guidance

  • Should longitudes outside [-180, 180] be rejected, or accepted as they are?
  • Is the transformer stateless, so that transform works without a prior call to fit ?
  • For the east-west leg, is the haversine distance between the two points at the pickup latitude what you want, or the arc length along that parallel?

What a Strong Answer Covers Guidance

  • A fully vectorized haversine with its argument kept in range so antipodal rows stay finite.
  • The Manhattan decomposition with each leg measured exactly as specified: east-west at the pickup latitude, north-south at the pickup longitude.
  • The initial bearing from the two-argument arctangent, normalized into [0, 360), with identical points and antimeridian crossings handled.
  • Row-local NaN propagation and the required validation: TypeError , missing columns, and the latitude range check that skips NaN.
  • A new float64 frame on the input index, in the required column order, with X left untouched.

Follow-up Questions Guidance

  • How would you make this a drop-in scikit-learn transformer?
  • Why can a city grid's Manhattan distance differ from this lat/lon decomposition?
  • How would you feed bearing to a model given its circular nature?