Reported July 2025
Capillary Technologiestree

Top View of a Binary Tree

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

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

The Capillary Technologies OA reported in July 2025 hands you a top view of a binary tree, and it's really a BFS with a map keyed by horizontal distance. The twist is the input. It's a level-order array with "null" strings, so you have to rebuild parent-child links before you can do anything. Once the tree exists, the rest is short. If you blank on the deserialization or the ordering, StealthCoder is the invisible safety net that reads the problem on screen and gives you a working solution during the live assessment. Know the shape first, though.

The problem

You are given a binary tree serialized in level order as levelOrder. Each entry is either an integer written as a string or "null".
Assign horizontal distance 0 to the root. A left child has its parent's distance minus 1, and a right child has its parent's distance plus 1. For each horizontal distance, the first node reached in level order is visible from above.
Return the visible node values from the smallest horizontal distance to the largest. Return an empty array for an empty tree.

Function
topView(levelOrder: String[]) → int[]

Examples
Example 1
levelOrder = ["1","2","3","null","4","null","5"]
return = [2,1,3,5]
The first visible nodes at horizontal distances -1, 0, 1, and 2 are 2, 1, 3, and 5.
Example 2
levelOrder = ["10","5","15","2","7","12","20"]
return = [2,5,10,15,20]
The topmost values are read from the far-left horizontal distance through the far-right distance.
Example 3
levelOrder = []
return = []
An empty tree has no visible nodes.

Constraints
0 <= levelOrder.length <= 10^5
Every non-null entry is a valid 32-bit signed integer.
The serialization describes one valid binary tree in level order.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: do a BFS and track a horizontal distance for each node. Root is 0, left child is minus 1, right child is plus 1. The first time you see a distance, store that node's value. Later nodes at the same distance are hidden. Because BFS goes level by level, first seen means topmost. Track min and max distance, then read the map from min to max. That avoids a sort. The common pitfall is using DFS, which can record a deeper node before a shallower one at the same distance. The second pitfall is the parsing. Use a queue of nodes and an index into the array. For each dequeued node, consume the next two entries as left and right, skipping "null". Handle the empty array early. With 10^5 entries, keep it O(n) and iterative. If the parsing or ordering trips you up live, StealthCoder is the hedge.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Top View of a Binary Tree 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Capillary Technologies reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Top View of a Binary Tree FAQ

What's the trick in Top View of a Binary Tree?+

Use BFS and assign each node a horizontal distance, left minus 1 and right plus 1. Record the value only the first time a distance appears. BFS guarantees the first one is the topmost. Then output distances from smallest to largest.

Why does DFS fail here?+

DFS can reach a deep node at some distance before a shallower node at the same distance. You'd store the wrong value unless you also track depth and compare. BFS avoids that because it visits level by level, so first seen wins.

How do I build the tree from the level-order array with nulls?+

Make the root from index 0, push it on a queue, and keep a pointer at index 1. For each dequeued node, read the next two entries as left and right. Create a child unless the entry is "null", and enqueue real children. You can also skip building nodes and carry distances in the queue.

How do I avoid sorting the results?+

Track the minimum and maximum horizontal distance while traversing. Store first values in a hash map keyed by distance. After BFS, loop from min to max and append each value. That's O(n) with no sort needed.

How should I prepare in 48 hours for this Capillary Technologies OA?+

Write the top view solution from scratch twice, including the null-aware level-order parsing. Test the empty array, a single node, and a skewed tree. Then do the bottom view and vertical order variants, since they use the same BFS with distance map and only change the update rule.

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

OA at Capillary Technologies?
Invisible during screen share
Get it