Design an In-Memory Ride-Sharing System

Difficulty: EASYCategory: LLDFrequency: 1Asked On 2025-06-06

Problem Statement

Design and implement a simplified in-memory ride-sharing system (like Uber or Ola), focusing on clean, extensible, and modular design principles. The system should support driver and rider interactions, dynamic pricing, and future extensibility, all while maintaining real-time availability and matchmaking in memory.


🎯 Functional Requirements

Implement the following core APIs:

1.DriverGoOnline(driverId, location, currentTime)
•Marks a driver as available and online at a specific location and time.
2.RiderRequest(source, destination, currentTime) → (driverId, price, timeOfPickup)
•Matches the rider with the nearest available driver.
•Returns:
•driverId
•calculated price
•estimated time of pickup
3.DriverGoOffline(driverId, currentTime)
•Marks the driver as unavailable or offline.


🧱 Data Modeling

Driver

•driverId
•Location location
•boolean isAvailable
•long availableSince
•long busyUntil

Rider

•riderId
•Location source
•Location destination

Location

•double x, double y


🔁 Follow-up Extensions

🔸 Dynamic Pricing (Follow-up 1)

•If fewer than 3 drivers are available system-wide, apply 1.5x surcharge.

🔸 Driver Queueing (Follow-up 2)

•If no drivers are currently available, assign the ride to the first driver who becomes free, even if the rider has to wait.

🔸 Extensible Pricing Engine (Follow-up 3)

•Implement a PricingStrategy interface and allow multiple pricing rules:
•Time of day
•Number of available drivers
•Surge based on location and recent demand


🧠 Suggested Class Design

Interfaces

1java 2CopyEdit 3interface PricingStrategy { 4 double calculatePrice(Location source, Location dest, int availableDrivers, LocalDateTime requestTime); 5}

Core Services

•DriverService
•Tracks online/offline status
•Manages driver availability and locations
•RideMatchingService
•Finds closest available driver
•Implements fallback queueing if no drivers available
•PricingService
•Delegates to PricingStrategy implementation(s)
•RideService
•Coordinates ride request lifecycle


🧪 Example Test Scenario

1plaintext 2CopyEdit 3DriverGoOnline("D1", (0,0), 9:00 AM) 4DriverGoOnline("D2", (5,5), 9:01 AM) 5RiderRequest((1,1), (10,10), 9:02 AM) 6 → assigned to D1, price = base × 1.0, timeOfPickup = 1 min 7 8DriverGoOffline("D1", 9:03 AM) 9RiderRequest((2,2), (12,12), 9:04 AM) 10 → assigned to D2 11 12DriverGoOffline("D2", 9:05 AM) 13RiderRequest((1,1), (5,5), 9:06 AM) 14 → no drivers available → wait for D1 or D2 (whichever becomes free first)


🔐 Non-Functional Requirements

•Thread-safe: The system must support concurrent rider and driver actions (e.g., multiple riders requesting at once).
•In-memory only: No persistence layer needed; data structures must be optimized for performance.
•Clean design principles: SOLID, interface-driven, separation of concerns.
•Extensibility: Easy to plug in more pricing or matching strategies.


✅ Evaluation Criteria

•Correctness: Accurate matchmaking and pricing with valid fallback logic.
•Extensibility: Flexible strategy-based pricing and scalable driver handling.
•Concurrency: Thread-safe design for live dispatch and updates.
•Design patterns: Use of Strategy, Factory, Interface Segregation, and Dependency Injection.
•Testing & Validation: Good test coverage for happy and edge cases.


✨ Optional Enhancements

•Track driver history or completed rides
•Add a Ride entity for status tracking
•Implement zone-based pricing (e.g., high-traffic areas)
•Allow riders to cancel a request
•ETA calculation based on speed


Solution

Comments