Flag Fuel Card Purchases Made Far From the Assigned Vehicle
Last updated: November 29, 2026
Quick Overview
Given fuel card transactions with merchant coordinates and a table of vehicle GPS pings, write SQL that matches each transaction to the vehicle's closest ping within 15 minutes and labels it far_from_vehicle (more than 2 miles away), no_location, or ok.
Motive
November 29, 20260
0
0 solved
Given fuel card transactions with merchant coordinates and a table of vehicle GPS pings, write SQL that matches each transaction to the vehicle's closest ping within 15 minutes and labels it far_from_vehicle (more than 2 miles away), no_location, or ok.
Motive Card ties spend to telematics, and one of its strongest fraud signals is a fuel purchase where the truck is nowhere near the pump. This screen checks whether you can do a time-windowed nearest-neighbor join in SQL, keep unmatched rows, and reason about what a missing ping means.
What the Interviewer Expects
- Join on vehicle and a bounded time window rather than a cross join
- Pick the nearest ping per transaction with row_number
- Compute great-circle distance, not raw degree differences
- Use a left join so transactions with no ping are labeled instead of dropped
- Discuss index and partitioning needs on the ping table
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you also flag gallons greater than the vehicle's tank capacity?
- How would you run this continuously at authorization time rather than in a batch?
- What would you do about vehicles with no gateway installed?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
card_transactions(txn_id, card_id, vehicle_id, txn_time, merchant_lat, merchant_lon, gallons) and vehicle_pings(vehicle_id, ping_time, lat, lon). I would confirm both timestamps are UTC, that `veh...
Approach
Join each transaction to its vehicle's pings within plus or minus 15 minutes, rank candidates by absolute time difference, and keep rank 1. Compute haversine distance in miles from that ping to the me...