Company: Uber

Difficulty: hard

Problem Statement

# Prime Pairs in a Tree You are given an undirected tree with `N` vertices numbered from `1` to `N` and rooted at vertex `1`. Count unordered pairs of distinct vertices `(u, v)` such that the unique simple path from `u` to `v` contains exactly one vertex whose label is a prime number. The endpoints are included in the path. Pairs `(u, v)` and `(v, u)` are the same. ## Input Format - The first line contains `N`. - Each of the next `N - 1` lines contains two integers `u` and `v`, describing an edge. ## Output Format Print one integer: the number of valid unordered pairs. ## Constraints - `1 <= N <= 100000` - `1 <= u, v <= N` - The edges form a tree. - The answer may exceed 32-bit signed integer range; use 64-bit arithmetic. ## Example Input: 7 1 2 1 3 2 6 3 4 3 5 4 7 Output: 7 ## Notes - `1` is not prime. - A prime-labeled endpoint counts as a prime on the path. - Vertices in a pair must be distinct.

More Uber OA questionsInterview experiences