16.3k views
2 votes
Rideshare companies like Uber or Lyft track the x,y coordinates of drivers and customers on a map. If a customer requests a ride, the company's app estimates the minutes until the nearest driver can arrive. Write a method that, given the x and y coordinates of a customer and the three nearest drivers, returns the estimated pickup time. Assume drivers can only drive in the x or y directions (not diagonal), and each mile takes 3.5 minutes to drive. All values are doubles; the coordinates of the user and of the drivers are stored as arrays of length 2. 289222.1780078.qx3zqy7

User Indregaard
by
5.1k points

2 Answers

5 votes

Final answer:

To determine the estimated pickup time for a rideshare customer, calculate the Manhattan distance between the customer and each driver, multiply the shortest distance by 3.5 minutes per mile to find the time, and return the minimum time.

Step-by-step explanation:

To calculate the estimated pickup time for a rideshare customer, we need to measure the distance in the x and y directions between the customer and each driver, then determine the time it would take for the closest driver to reach the customer. Given that each driver can only move in the x or y direction and each mile takes 3.5 minutes to drive, we can use the Manhattan distance metric (sum of absolute differences in the x and y coordinates) to determine the distance and then calculate the time. The Manhattan distance is highly appropriate for urban areas where drivers navigate a grid-like pattern of streets.

Here is a method in pseudocode that performs this calculation:

function estimatePickupTime(customerCoordinates, driverCoordinates):
closestTime = Infinity
for driver in driverCoordinates:
distanceX = abs(customerCoordinates[0] - driver[0])
distanceY = abs(customerCoordinates[1] - driver[1])
totalDistance = distanceX + distanceY
time = totalDistance * 3.5
if time < closestTime:
closestTime = time
return closestTime

When this function is used, it will iterate through the list of drivers, calculate how long it would take each driver to reach the customer, and then return the shortest estimate.

User Mishalhaneef
by
5.6k points
6 votes

Answer:

The program in Java is as follows:

import java.util.*;

public class Main{

public static void main (String[]args){

Scanner input = new Scanner(System.in);

double user[] = new double[2]; double dr1[] = new double[2];

double dr2[] = new double[2]; double dr3[] = new double[2];

System.out.print("Enter user coordinates: ");

for(int i =0;i<2;i++){ user[i] = input.nextDouble(); }

System.out.print("Enter driver 1 coordinates: ");

for(int i =0;i<2;i++){ dr1[i] = input.nextDouble(); }

System.out.print("Enter driver 2 coordinates: ");

for(int i =0;i<2;i++){ dr2[i] = input.nextDouble(); }

System.out.print("Enter driver 3 coordinates: ");

for(int i =0;i<2;i++){ dr3[i] = input.nextDouble(); }

double dr1dist = Math.abs(user[0] - dr1[0]) + Math.abs(user[1] - dr1[1]);

double dr2dist = Math.abs(user[0] - dr2[0]) + Math.abs(user[1] - dr2[1]);

double dr3dist = Math.abs(user[0] - dr3[0]) + Math.abs(user[1] - dr3[1]);

System.out.println("Estimated pickup time of driver 1 "+(3.5 * dr1dist)+" minutes");

System.out.println("Estimated pickup time of driver 2 "+(3.5 * dr2dist)+" minutes");

System.out.println("Estimated pickup time of driver 3 "+(3.5 * dr3dist)+" minutes");

}

}

Step-by-step explanation:

The following array declarations are for the customer and the three drivers

double user[] = new double[2]; double dr1[] = new double[2];

double dr2[] = new double[2]; double dr3[] = new double[2];

This prompts the user for the customer's coordinates

System.out.print("Enter user coordinates: ");

This gets the customer's coordinate

for(int i =0;i<2;i++){ user[i] = input.nextDouble(); }

This prompts the user for the driver 1 coordinates

System.out.print("Enter driver 1 coordinates: ");

This gets the driver 1's coordinate

for(int i =0;i<2;i++){ dr1[i] = input.nextDouble(); }

This prompts the user for the driver 2 coordinates

System.out.print("Enter driver 2 coordinates: ");

This gets the driver 2's coordinate

for(int i =0;i<2;i++){ dr2[i] = input.nextDouble(); }

This prompts the user for the driver 3 coordinates

System.out.print("Enter driver 3 coordinates: ");

This gets the driver 3's coordinate

for(int i =0;i<2;i++){ dr3[i] = input.nextDouble(); }

This calculates the distance between driver 1 and the customer

double dr1dist = Math.abs(user[0] - dr1[0]) + Math.abs(user[1] - dr1[1]);

This calculates the distance between driver 2 and the customer

double dr2dist = Math.abs(user[0] - dr2[0]) + Math.abs(user[1] - dr2[1]);

This calculates the distance between driver 3 and the customer

double dr3dist = Math.abs(user[0] - dr3[0]) + Math.abs(user[1] - dr3[1]);

The following print statements print the estimated pickup time of each driver

System.out.println("Estimated pickup time of driver 1 "+(3.5 * dr1dist)+" minutes");

System.out.println("Estimated pickup time of driver 2 "+(3.5 * dr2dist)+" minutes");

System.out.println("Estimated pickup time of driver 3 "+(3.5 * dr3dist)+" minutes");

User Michael Shigorin
by
5.5k points