← 2023 questions

2023 / GRAB / QUESTION 23

Distance and fares

Try the question in your own editor, then check your output below.

THE QUESTION

Tristan would need to know the distance between the place he is standing right now and his destination in order to find out the Grab fare that he has to pay. Create a function in which it will takes two different set of longitude and latitude (staring point and destination) and return the displacement between the locations and calculate the fare using the following scheme:

The base fare is $0.75

For each next 10km the fare is $0.5/km

For each km after the first 10km, the fare is $0.35/km

Question by Phuong Nghi. Source: Girls In Code SEA — Behind the Screens, 2023, slide 40.

PRACTICE CHECK

Try your answer.

This checkpoint supplies distance directly to check the fare bands; the complete exercise also needs coordinate-to-distance conversion.

Run your program in your own editor, then paste its output below. This checks the stated output or checkpoint; it does not assess your full program. Practice attempts do not affect competition scores.

Practice distance is 12 km. Use base $0.75, $0.50/km for the first 10 km and $0.35/km after that. Enter the fare to 2 decimal places.

Reveal worked answer

Fare

6.45

0.75 + 10×0.50 + 2×0.35 = 6.45.