Apple SWE Phone Screen | 60L CTC

Anonymous User
338
Feb 28, 2026

You are given a tree consisting of N nodes numbered from 1 to N.

The value of each node is equal to its index.

You are also given an integer k.

A path in the tree is called a valid AP path if:

The path contains distinct nodes.

Adjacent nodes are connected by an edge.

For every adjacent pair (u, v) in the path:

v - u = k OR v - u = -k

The path may start and end at any nodes and may bend at any node.

Your task is to compute the maximum number of nodes in any valid AP path.
Sample Input
1
6 2
1 3
3 5
5 2
2 4
4 6
Sample Output
3
Explanation (brief)

k = 2

Valid edges (|u − v| = 2):

1 – 3

3 – 5

2 – 4

4 – 6

So we get two valid chains:

1 → 3 → 5

2 → 4 → 6

Maximum length = 3.

Comments (1)