I knew the Haversine formula existed but had never actually coded it from scratch.
Start by clarifying the command's interface and error handling, then outline the Haversine formula step by step, emphasizing validation and rounding. Write clean, modular code with helper functions for validation and distance calculation, and test with edge cases like poles and the antimeridian.
Pro tip: Mention that you'll use the haversine formula instead of the spherical law of cosines to avoid precision issues for small distances, and explicitly handle floating-point rounding with a standard round-half-up approach.
Confirm input format (e.g., 'DISTANCE lat1 lon1 lat2 lon2'), output format (integer km or ERROR), and any constraints like handling extra spaces or case sensitivity.
Check that latitudes are within [-90, 90] and longitudes within [-180, 180]; if any are invalid, return ERROR immediately.
Convert degrees to radians, compute the central angle using the haversine function, then multiply by Earth's mean radius (6371 km) to get distance.
Round the computed distance to the nearest integer using standard rounding (e.g., Math.round) and return it as the output.
Verify with known distances (e.g., same point, antipodal points, equator/prime meridian) and invalid inputs to ensure correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.