πŸš– Uber - Surge Pricing Service

Real-time Pricing Based on Demand & Supply

1 hour
easy
5 submissions

Project Overview

Real-time Pricing Based on Demand & Supply

Uber wants to optimize its surge pricing algorithm to balance ride demand and driver availability in real-time. Your task is to build a Surge Pricing Microservice that calculates and returns a surge multiplier for ride fares based on demand and supply conditions in a specific location.

This service will be queried by the Ride Request Service to determine the appropriate fare adjustments during peak hours or high-demand situations.

Read the uber project microservices overview beforehand to understand how this fits in the architecture of the entire microservice.

Your submissions

No submissions yet, start by making your first submission

Detailed Project Description

Read the uber project microservices overview beforehand to understand how this fits in the architecture of the entire microservice.

🎯 Key Objectives

  • Your API should run on the port 5001.
  • Build an API that calculates and returns a surge multiplier based on active ride requests and available drivers.
  • Ensure real-time pricing updates by dynamically adjusting surge values.
  • Integrate with other microservices (Ride Request, Driver Management) to fetch required data.
  • Implement a logic-driven pricing model to ensure fairness and prevent price abuse.

πŸ“ Required API Endpoints

Your Surge Pricing Service API should expose the following endpoints:

1️⃣ Get Surge Multiplier for a Location

  • Endpoint: GET /surge?lat={lat}&lng={lng}
  • Description: Returns the surge multiplier for the given location.
  • Request Parameters:
    • lat (float) β†’ Latitude of the location.
    • lng (float) β†’ Longitude of the location.
  • Response:
    { "location": { "lat": 40.7128, "lng": -74.006 }, "activeRequests": 15, "availableDrivers": 5, "surge_multiplier": 2.0 }
  • Logic:
    • If available drivers = 0, return maximum surge multiplier (e.g., 2.5).
    • If demand/supply ratio ≀ 1, return 1.0 (no surge).
    • If demand/supply ratio > 1.5, increase the multiplier gradually (e.g., 1.2, 1.5, 2.0).
    • Ensure the multiplier never exceeds a maximum cap to prevent extreme pricing.

βš™οΈ How the Surge Pricing Logic Works

  1. Tracking Demand & Supply:

    • The system monitors active ride requests and available drivers in real-time.
    • Data is stored per geographical location (lat, lng).
  2. Calculating Surge Multiplier:

    • Demand-Supply Ratio = active ride requests / available drivers
    • If supply is low, increase the multiplier.
    • If supply is high, keep the multiplier at 1.0 (no surge).
  3. Integration with Ride Request Service:

    • When a user requests a ride, the Ride Request Service queries this service.
    • The multiplier is applied to the fare calculation to adjust pricing.

πŸ“Œ Expected Behavior

ScenarioExpected Surge Multiplier
Low demand, many drivers1.0 (No surge)
Moderate demand, balanced drivers1.2 - 1.5 (Mild surge)
High demand, few drivers2.0 (Strong surge)
No available drivers2.5 (Max Surge)

πŸš€ Deliverables

  • A fully functional Surge Pricing API with the defined endpoints.
  • Proper logic implementation for surge calculations.
  • Well-structured error handling and validations.
  • Your API should run on the port 5001.

πŸ“Œ By completing this project, you will create a real-world, scalable surge pricing engine used in ride-sharing applications like Uber! πŸš–πŸ’‘

Project Completion Criteria

  • The system should allow users to retrieve a surge multiplier for a given location via the GET /surge?lat={lat}&lng={lng} endpoint.
  • The system should dynamically calculate surge pricing based on the ratio of active ride requests to available drivers.
  • The system should ensure that if no drivers are available, the maximum surge multiplier is applied (e.g., 2.5).
  • The system should return a surge multiplier of 1.0 when demand and supply are balanced.