Reported September 2026
IBMbreadth first search

Tree Pythagorean Triples

Reported by candidates from IBM's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live IBM OA. Under 2s to a working solution.
Founder's read

The IBM OA reported in September 2026 looks like a geometry problem, but it's a tree traversal with a filter on top. You get a tree of up to 1000 nodes and three fixed vertices. For every vertex you need its distance to each one, then check for a Pythagorean triple. It's a BFS problem wearing a math costume. Once you see that, the code is short. If you blank on the setup during the live OA, StealthCoder runs invisibly as a safety net and hands you the structure. Here's the shape of it before you open the invite.

The problem

You are given an undirected tree with vertices numbered from 1 through n, along with three fixed vertices x, y, and z.
For every vertex v, compute its unweighted edge distances to x, y, and z. Sort those three distances as a <= b <= c. Count the vertices for which all three distances are positive and a * a + b * b == c * c.
The fixed vertices may coincide. A vertex with distance zero to any of them does not contribute to the answer.

Function
countPythagoreanVertices(n: int, edges: int[][], x: int, y: int, z: int) → int

Examples
Example 1
n = 9
edges = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9]]
x = 1
y = 8
z = 9
return = 1
Vertex 4 has distances 3, 4, and 5, so it contributes. No other vertex forms a positive Pythagorean triple.
Example 2
n = 4
edges = [[1,2],[1,3],[1,4]]
x = 2
y = 3
z = 4
return = 0
The center has distances 1, 1, and 1. Every leaf has one zero distance, so the answer is zero.
Example 3
n = 13
edges = [[1,2],[2,3],[3,4],[1,5],[5,6],[6,7],[7,8],[1,9],[9,10],[10,11],[11,12],[12,13]]
x = 4
y = 8
z = 13
return = 1
The three branches give the center distances 3, 4, and 5. The traversal checks every vertex rather than only the branch point.

Constraints
1 <= n <= 1000.
edges.length == n - 1.
Every edge contains two distinct vertex IDs in [1, n], and the edges form one connected tree.
1 <= x, y, z <= n; the three fixed vertices may coincide.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to stop thinking per vertex. Run BFS three times, once from x, once from y, once from z, and store three distance arrays. That's O(n) per run, so O(n) total. No pairwise distance calls, no LCA, no brute force over vertex triples. Then loop over all vertices, skip any with a zero distance, sort the three values as a <= b <= c, and test a*a + b*b == c*c. The common pitfalls are forgetting that x, y, z may coincide, which just means two arrays are identical and the logic still works, and checking only branch points instead of every vertex. Example 3 warns about exactly that. Build an adjacency list from the edge list, use 1-indexed arrays, and count carefully. If the live OA rattles you, StealthCoder is the hedge that keeps the BFS template in front of you.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Tree Pythagorean Triples cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass IBM's OA.

IBM reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Tree Pythagorean Triples FAQ

How hard is Tree Pythagorean Triples really?+

Easy to medium. The tree is small, n is at most 1000, and the core is three BFS runs plus a per-vertex check. The difficulty is reading the statement carefully, not the algorithm. Most people lose points on the zero-distance rule, not on the traversal.

What's the trick to solving it fast?+

Compute distances from each of x, y, and z once with BFS, giving three arrays. Then scan every vertex, sort its three distances, and test a^2 + b^2 == c^2. No need for LCA or pairwise distance queries. Total work is linear in n.

What happens when x, y, and z are the same vertex?+

Nothing special. The three distance arrays come out identical, so every vertex has equal distances. The root vertex has zero distance and is skipped. Equal positive values a, a, a never satisfy a^2 + a^2 == a^2, so the answer is zero. No special-casing needed.

Do I need to handle vertices with a zero distance separately?+

Yes, but it's one condition. If any of the three distances is zero, skip that vertex. A distance of zero means the vertex is one of the fixed ones, and the problem says those don't count. Check it before the Pythagorean test.

How do I prepare for this in 48 hours?+

Write a BFS on an adjacency list from memory until it's automatic. Then practice the pattern of running BFS from several sources and combining the distance arrays. Test on the three examples, especially the star tree and the path. That's enough for this problem.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with IBM.

OA at IBM?
Invisible during screen share
Get it