Search⌘ K
AI Features

Frog Position After T Seconds

Understand how to determine the probability that a frog, starting from vertex one, reaches a target vertex on a tree after t seconds. Explore tree breadth-first search traversal and probabilistic movement rules to solve this coding interview problem effectively using C++.

Statement

You are given an undirected tree with n vertices labeled from 11 to nn. A frog starts at vertex 11, at time 00, and makes one move per second.

At each step, the frog follows these rules:

  1. Move to an unvisited neighbor:
    If the frog has unvisited neighbors, it jumps to one of them, chosen uniformly at random (equal probability for each choice).

  2. No revisiting:
    The frog can not jump back to a vertex it has already visited.

  3. Stay when stuck
    The frog will keep jumping at its current vertex if there are no unvisited neighbors.

The tree is represented as an array of edges, where edges[i] = [aia_i ...