Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Markov Chains and Steady State

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Markov Chains and Steady State. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Upstart models application states with a finite Markov chain, where each row of a transition matrix gives the next-state probabilities. Given an initial distribution, simulate transitions until the distribution converges to the chain's steady-state distribution.

Implement find_steady_state, returning the converged probability distribution. Use an L1 distance between consecutive distributions to determine convergence.

Formal Specification

Input consists of:

  1. matrix, an n x n list of lists where matrix[i][j] is the probability of transitioning from state i to state j.
  2. initial_distribution, a length-n probability vector.
  3. max_iterations, the maximum number of matrix-vector updates.
  4. tolerance, the required maximum L1 distance between successive distributions.

Return a length-n list representing the steady-state distribution. Round returned values to 10 decimal places. The matrix is guaranteed to describe an irreducible, aperiodic Markov chain, so the steady state is unique and iterative simulation converges within max_iterations.

Constraints

  • 1 <= n <= 50
  • matrix is an n x n row-stochastic matrix
  • Every matrix probability is in [0, 1]
  • initial_distribution has length n and sums to 1 within 1e-12
  • 1 <= max_iterations <= 1,000,000
  • 0 < tolerance < 1
  • The chain is irreducible and aperiodic
  • Convergence occurs within max_iterations

Function Signature

def find_steady_state(matrix, initial_distribution, max_iterations, tolerance):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output