Number of Good Paths
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given a tree with n nodes labeled 0 to n - 1, an array vals where vals[i] is the value of node i, and an array edges of n - 1 undirected edges [a, b].
A **good path** is a simple path whose two endpoints have the **same value**, and where every node on the path (including the endpoints) has a value **no greater than** that endpoint value.
Return the number of distinct good paths. A path and its reverse count once, and a single node by itself is a good path.
Examples
Constraints
- `1 <= n <= 3 * 10^4`
- `vals.length == n`
- `0 <= vals[i] <= 10^5`
- `edges.length == n - 1`
- `edges` forms a valid tree
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.