Support two basic uber features:
- Drivers report their locations.
- Rider request a uber, return a matched driver.
When rider request a uber, match a closest available driver with him, then mark the driver not available.
When next time this matched driver report his location, return with the rider's information.
You can implement it with the following instructions:
report(driver_id, lat, lng)
1) return null if no matched rider.
2) return matched trip information.request(rider_id, lat, lng)
1) create a trip with rider's information.
2) find a closest driver, mark this driver not available.
3) fill driver_id into this trip.
4) return trip
This is the definition of_Trip_in Java:
public class Trip {
public int id; // trip's id, primary key
public int driver_id, rider_id; // foreign key
public double lat, lng; // pick up location
}
Have you met this question in a real interview?
Yes
Example
report(1, 36.1344, 77.5672) // return null
report(2, 45.1344, 76.5672) // return null
request(2, 39.1344, 76.5672) // return a trip, LOG(INFO): Trip(rider_id: 2, driver_id: 1, lat: 39.1344, lng: 76.5672)
report(1, 38.1344, 75.5672) // return a trip, LOG(INFO): Trip(rider_id: 2, driver_id: 1, lat: 39.1344, lng: 76.5672)
report(2, 45.1344, 76.5672) // return null
/**
* Definition of Trip:
* class Trip {
* public:
* int id; // trip's id, primary key
* int driver_id, rider_id; // foreign key
* double lat, lng; // pick up location
* Trip(int rider, double lat, double lng);
* }
* Definition of Helper
* class Helper {
* public:
* static get_distance(double lat1, double lng1,
* double lat2, double lng2) {
* // return distance between (lat1, lng1) and (lat2, lng2)
* }
* };
*/
#include <memory>
using namespace std;
class Location {
public:
double lat, lng;
Location(double _lat = .0, double _lng = .0) :lat(_lat), lng(_lng) {};
};
class MiniUber {
private:
map<int, Location> driver2location;
map<int, unique_ptr<Trip> > driver2trip;
public:
MiniUber() {
// initialize your data structure here.
}
// @param driver_id an integer
// @param lat, lng driver's location
// return matched trip information if there have matched rider or NULL
Trip* report(int driver_id, double lat, double lng) {
// Write your code here
if (driver2trip.find(driver_id) != driver2trip.end()) {
return driver2trip[driver_id].get();
}
driver2location[driver_id] = Location(lat, lng);
return nullptr;
}
// @param rider_id an integer
// @param lat, lng rider's location
// return a trip
Trip* request(int rider_id, double lat, double lng) {
// Write your code here
Trip* trip = new Trip(rider_id, lat, lng);
int driver_id = -1;
double distance = -1;
for (auto& it : driver2location) {
double curDis = Helper::get_distance(lat, lng, it.second.lat, it.second.lng);
if (distance < 0 || curDis < distance) {
distance = curDis;
driver_id = it.first;
}
}
if (driver2location.find(driver_id) != driver2location.end())
driver2location.erase(driver_id);
trip->driver_id = driver_id;
driver2trip[driver_id].reset(trip);
return trip;
}
};