Expected City Arrival and Return Days
Updated
Problem statement
A traveler moves to a city once per day, following the chances given by transitionWeights. Row i holds the weights for moves out of city i. You also get a city index, target. Return one expected number of days for every starting city.
If the traveler starts in a city other than target, count the days until the traveler first reaches the target. Then stop.
If the traveler starts in the target on day zero, the traveler still makes at least one normal daily move. Count the days until the traveler is back in the target. Staying there on day one counts as a return after one day. The traveler may leave the target, but does not have to.
The number of cities is the number of rows in transitionWeights. Two different cities always have a positive weight between them, so every expected time is finite. Complete expectedCityArrivalDays(transitionWeights,target).
Each returned value is accepted when its absolute error is at most 1e-7 * max(1, abs(expected)). In other words, the tolerance is 1e-7, either absolute or relative.
Function
expectedCityArrivalDays(transitionWeights: int[][], target: int) → double[]Examples
Example 1
transitionWeights = [[2,1,1],[1,0,1],[1,1,2]]target = 0return = [2.5,2.6666666666666665,3.3333333333333335]Call the three cities NYC, Nashville and SF, in input order. NYC is the target. The expected days are [5/2,8/3,10/3]. Let H_N be the expected days from Nashville to NYC. Let H_S be the same for SF. Nashville moves to NYC or SF, each half the time, so H_N=1+H_S/2. SF moves to NYC or Nashville a quarter of the time each, and stays half the time. So H_S=1+H_N/4+H_S/2. Solving these two gives the second and third values. NYC stays half the time, which is a return after one day. It moves to each other city a quarter of the time. So the return trip from NYC takes 1+H_N/4+H_S/4=5/2 days.
Example 2
transitionWeights = [[1]]target = 0return = [1.0]The only city is the target. The traveler stays there on day one, so the return takes one day.
Example 3
transitionWeights = [[0,1],[1,0]]target = 0return = [2.0,1.0]From city one, the traveler always moves to the target, city zero, so that takes one day. From city zero, the traveler must first go to city one and then come back. That return takes two days.
Example 4
transitionWeights = [[0,3],[1,2]]target = 0return = [4.0,3.0]From city one, each day the traveler reaches city zero with chance one third. So the expected wait is three days. From city zero, the traveler always moves to city one first. That takes one day, plus three expected days to come back, for four days in total.
Constraints
- There are
1 <= n <= 10cities. The weights form a square matrix of integers from0through100. - For
n>1, every weight between two different cities is at least one. A city's weight to itself may be zero. With one city, its only weight is positive. - Each row gives its own chances. The chance of moving from city
ito cityjisP[i][j]=transitionWeights[i][j]/sum(transitionWeights[i]). - Return the answers in input city order. Do not change the inputs.
0 <= target < n.
More IMC Trading Phone Screen questions
- Election District Budget Allocation AnalysisPHONE SCREEN · Seen Aug 2026
- Uniform Variables: Smaller Below Larger SquaredPHONE SCREEN · Seen Mar 2026
- Combination Sum with Unlimited ReusePHONE SCREEN · Seen Dec 2025
- Valid Range of a Three-Outcome Probability ParameterPHONE SCREEN · Seen Dec 2025
- Expected Fishing CatchPHONE SCREEN · Seen Dec 2025
- Count Routes Through a City GridPHONE SCREEN · Seen Nov 2024
- Stationary City Population SharesPHONE SCREEN · Seen Nov 2024
- Maximum Falling Path SumPHONE SCREEN · Seen Nov 2024
- Trading Profit Percentage ChangePHONE SCREEN · Seen Feb 2024
- Poker Suit Expected ProfitPHONE SCREEN · Seen Sep 2023
Similar questions at other companies
- Sliding Window Attention over a KV CacheNVIDIA · PHONE SCREENNVIDIA interview questions
- Integers With Multiple Sum of Two CubesCharta Health · PHONE SCREEN · ONSITE INTERVIEWCharta Health interview questions
- Dice-Walk Winning ProbabilityGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Logistic Regression with Gradient DescentYahoo · PHONE SCREENYahoo interview questions
- Evaluate an Arithmetic Expression Written in WordsBrex · PHONE SCREEN
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Integer to English Words in USD and INR StylesVeeam · PHONE SCREEN
- Adaptive Calibration ErrorOpenAI · PHONE SCREENOpenAI interview questions
- Sum of Smallest Non-DivisorsThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Square Root FunctionCitadel · PHONE SCREENCitadel interview questions