HARDasked at 1 company

Maximize the Number of Target Nodes After Connecting Trees II

A hard-tier problem at 73% community acceptance, tagged with Tree, Depth-First Search, Breadth-First Search. Reported in interviews at jio and 0 others.

Founder's read

You're given two trees and need to connect them to maximize target nodes. This problem shows up in live OAs even at smaller scale, and it's a trap. The naive greedy approach feels right but fails fast. You'll think you can just pick the connection point that maximizes targets locally, then realize you missed the global optimization. Candidates typically brute-force every possible connection, which times out. The real pattern is BFS from each potential connection point to count targets in the merged tree, then pick the best one. If this hits your assessment and you blank on the counting trick, StealthCoder solves it in seconds, invisible to the proctor.

Companies asking
1
Difficulty
HARD
Acceptance
73%

Companies that ask "Maximize the Number of Target Nodes After Connecting Trees II"

If this hits your live OA

Maximize the Number of Target Nodes After Connecting Trees II is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by a senior engineer who knows the OA is theater. This is the script.

Get StealthCoder
What this means

The core trick is understanding that when you connect tree A to tree B at specific nodes, the target count for each tree changes based on the connection point. You can't just count targets in isolation. You need to simulate each connection option: remove the edge from A's root, attach A at a candidate node in B, then count all reachable target nodes via BFS. Do this for every pair of candidate nodes. The pattern most candidates miss is that you're not optimizing locally at each node; you're testing all pairwise connections and picking the absolute best merge. Common pitfall: forgetting that connecting A to B at node X in B means targets in A are still counted, but now they're only reachable if X is reachable from B's root. The problem is HARD because the state space is large and BFS traversal after each hypothetical connection adds overhead. When you're stuck on which nodes to try as connection points, StealthCoder runs the full simulation and returns the answer, letting you paste working code.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Maximize the Number of Target Nodes After Connecting Trees II recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximize the Number of Target Nodes After Connecting Trees II interview FAQ

Is this really asked in live OAs, or is it a filter problem?+

It's reportedly asked by at least one company in active hiring. Accept rate sits at 73%, which is solid but not trivial. It's more likely a mid-to-late-round problem, not a first-pass filter. Expect it if you're interviewing at companies that dig into graph traversal and tree manipulation.

What's the actual trick I'm missing when I brute-force all connections?+

You're on the right track. The trick is that brute force works, but you need efficient counting. For each candidate connection, use BFS to count reachable targets in the merged tree. Don't try to optimize the connection choice itself; enumerate all O(n squared) pairs and pick the max. The overhead is manageable if your BFS is clean.

Do I need to handle the trees as rooted or unrooted?+

The problem specifies trees have roots. Treat them as rooted DAGs when counting targets. When you connect A to B, A's structure hangs off a node in B, and targets in A are reachable if that node is reachable from B's root. This is why simulation via BFS is cleaner than trying to reason about structure algebraically.

How does this relate to the Tree and DFS/BFS topics listed?+

Tree is the data structure. DFS or BFS is how you traverse the merged tree after each hypothetical connection to count targets. The problem itself is a search over connection options, so you'll use nested BFS/DFS. Most candidates use BFS for clarity since you're counting nodes reachable from the root of the merged tree.

What if the trees are huge and brute force times out?+

If n exceeds 10,000 or so, brute force will struggle. The optimal solution uses preprocessing: compute subtree target counts and reachability info, then answer each query in O(1). That's advanced and rarely expected live. If you hit timeout, brute force with tight BFS is usually the intended first pass.

Want the actual problem statement? View "Maximize the Number of Target Nodes After Connecting Trees II" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.