KatInterview

Ride Service Demand Under Weather Conditions

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashingprobabilitybayes-theoremconditional-probability

Last updated

Apply the Law of Total Probability and Bayes' Theorem to compute ride request likelihoods and conditional weather probabilities.

Problem

A transportation hub serves two types of commuters: those who typically use ride services and those who typically use public transit. Weather conditions influence whether commuters will actually request a ride on any given day.

You are given:

  • The probability that any day is rainy versus clear
  • The proportion of commuters who prefer ride services versus public transit
  • For rainy days: the fraction of ride-preferring commuters who request a ride, and the fraction of transit-preferring commuters who switch to requesting a ride
  • For clear days: the fraction of ride-preferring commuters who request a ride, and the fraction of transit-preferring commuters who switch to requesting a ride

Your task is to compute three quantities:

  1. Total request probability: The overall likelihood that a randomly selected commuter will request a ride on an arbitrary day
  2. Weather posterior: Given that a commuter requested a ride, the probability that the day is rainy
  3. Expected indicator: The expected value of an indicator variable X where X=1 if a commuter requests a ride and X=0 otherwise

Assume that commuter preference (ride service vs. transit) is independent of weather conditions.

Return a dictionary with keys "total_probability", "rain_given_lyft", and "expected_value", each rounded to four decimal places.

Constraints

  • 0 ≤ rain_prob ≤ 1
  • 0 ≤ lyft_pref_prob ≤ 1
  • 0 ≤ all conditional request/switch probabilities ≤ 1
  • If the total request probability is zero, return 0.0 for the weather posterior
  • All output values must be rounded to exactly 4 decimal places

Examples

Example 1

Input: calculate_ride_request_probabilities(0.25, 0.6, 0.8, 0.2, 0.3, 0.05)

Output: {'total_probability': 0.29, 'rain_given_lyft': 0.4828, 'expected_value': 0.29}

Example 2

Input: calculate_ride_request_probabilities(0, 0.6, 0.8, 0.2, 0.3, 0.05)

Output: {'total_probability': 0.2, 'rain_given_lyft': 0, 'expected_value': 0.2}

Example 3

Input: calculate_ride_request_probabilities(1, 0.6, 0.8, 0.2, 0.3, 0.05)

Output: {'total_probability': 0.56, 'rain_given_lyft': 1, 'expected_value': 0.56}

Hints

Hint 1

Use the Law of Total Probability by partitioning over both weather conditions and commuter preferences to find the overall request rate

Hint 2

Apply Bayes' Theorem to invert the conditional: multiply the likelihood of a request given rain by the prior probability of rain, then divide by the total request probability

Hint 3

The expected value of a binary indicator variable equals the probability that the variable is 1