FastPrepFrog Position After T Seconds
Problems/Frog Position After T Seconds

Frog Position After T Seconds

InMobi logoInMobi● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an InMobi phone screen. The report clearly gives the rules for a random walk on a tree. But its edge pairs are squeezed together, and both probabilities it shows do not fit those rules. Our examples are rebuilt separately. Our version is about 88% the same as the usable part of the report.

An undirected tree has n nodes labeled from 1 to n. A frog starts at node 1 at time 0.

Each second, the frog follows these rules:

  • If its current node has one or more neighbors it has not visited, it jumps to one of those neighbors with equal probability.
  • If every neighbor has already been visited, it remains at the current node.

The tree is supplied as an edge list, where every row [u, v] represents one undirected edge. Return the probability that the frog is at target after exactly t seconds.

Function

frogPosition(n: int, edges: int[][], target: int, t: int) → double

Examples

Example 1

n = 7edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]]target = 4t = 2return = 0.16666666666666666

The frog chooses node 2 from the root with probability 1/3, then chooses node 4 from node 2 with probability 1/2. Their product is 1/6.

Example 2

n = 4edges = [[1,2],[1,3],[1,4]]target = 4t = 2return = 0.3333333333333333

The frog reaches leaf 4 after one second with probability 1/3. Since the leaf has no unvisited neighbor, it stays there for the second second.

Constraints

  • 1 <= n <= 100
  • edges.length = n - 1
  • Every edge has exactly two endpoints in 1..n.
  • edges forms a connected, undirected tree.
  • 0 <= t <= n
  • 1 <= target <= n

More InMobi Phone Screen questions

  • House Robber IPHONE SCREEN · Seen Aug 2026
  • Word Break IIPHONE SCREEN · Seen Aug 2026
  • Bus Station Seat AllotmentPHONE SCREEN · Seen Mar 2026
  • Find Missing and Duplicate IntegerPHONE SCREEN · Seen Mar 2026
  • Frequency Equals Card ValuePHONE SCREEN · Seen Mar 2026
  • IPOPHONE SCREEN · Seen Mar 2026
  • Evaluate DivisionPHONE SCREEN · Seen Dec 2025
  • Compare Version NumbersPHONE SCREEN · Seen Feb 2022
  • Count Array DerangementsPHONE SCREEN · Seen Feb 2022
  • Maximum Frequency StackPHONE SCREEN · Seen Feb 2022

Similar questions at other companies

  • All Nodes Distance K in a Binary TreeAmazon · PHONE SCREEN
  • Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
  • Base Unit ConversionsAdobe · OAAdobe interview questions
  • Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Maximum Data Transfer TimeMicrosoft · OAMicrosoft interview questions
  • Longest Transfer Time in a Server TreeRippling · OARippling interview questions
  • Special Diameter EndpointsAgoda · OAAgoda interview questions
  • Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Evaluate DivisionApple · PHONE SCREENApple interview questions
  • Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
All InMobi interview questionsSee InMobi hiring insights
Saved
class Solution {
    public double frogPosition(int n, int[][] edges, int target, int t) {
        // write your code here
    }
}
Checking account…