Neural Network Subnetwork Strength
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks most first attempts at Microsoft's Neural Network Subnetwork Strength, reported in June 2026, is running a fresh search from every neuron. That's O(n^2) and it dies on big inputs. This is a tree problem in disguise. Each neuron gets a value of +1 if strong and -1 if weak, and you want the max-sum connected subgraph containing each node. It's the classic rerooting technique. If you blank on the second pass during the live OA, StealthCoder is the safety net running invisibly on your screen. Know the shape before you open the invite.
The problem
A neural network has n neurons numbered from 1 to n. If the ith neuron has strong connectivity, strongConnectivity[i] = 1. If it has weak connectivity, strongConnectivity[i] = 0. The neurons form a tree-like network with n - 1 connections, where the ith connection connects neurons neuronFrom[i] and neuronTo[i]. A neuron's strength is defined as the maximum difference between strongly connected and weakly connected neurons in any subnetwork including that neuron. Return an array of n integers, where the ith integer represents the strength of neuron i. Note: A subnetwork is a connected subgraph of the given network. Function getNeuronStrengths(n: int, neuronFrom: int[], neuronTo: int[], strongConnectivity: int[]) → int[] Examples Example 1 n = 4 neuronFrom = [1, 1, 1] neuronTo = [2, 3, 4] strongConnectivity = [0, 0, 1, 0] return = [0, -1, 1, -1] Neuron 3 has strong connectivity. For neuron 1's strength, consider the subnetwork with neurons 1 and 3: 1 strong, 1 weak, so strength is 0. For neuron 2's strength, consider the subnetwork with neurons 1, 2, and 3: 1 strong, 2 weak, so strength is -1. For neuron 3's strength, consider the subnetwork with only neuron 3: 1 strong, 0 weak, so strength is 1. For neuron 4's strength, consider the subnetwork with only neuron 4: 0 strong, 1 weak, so strength is -1. The neuronStrengths array is [0, -1, 1, -1].
Reported by candidates. Source: FastPrep
Pattern and pitfall
Map strong to +1 and weak to -1. Root the tree at neuron 1 and run a DFS computing down[v] = val[v] + sum of max(0, down[child]). That's the best subnetwork rooted at v going downward. Then reroot. For a child c of parent p, the parent's contribution excluding c is up = ans[p] - max(0, down[c]). Then ans[c] = down[c] + max(0, up). That's the second pass. The pitfall is forgetting the max(0,...) clamp, which forces you to include bad branches. Another trap is recursion depth on a path-shaped tree, so use an iterative DFS or BFS order. Check Example 1: neuron 2 gets -1 + max(0, 0 from neuron 1 and 3 side) giving -1. Total time is O(n). If the rerooting step slips under pressure, StealthCoder is the hedge on the live OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Neural Network Subnetwork Strength 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft 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.
Neural Network Subnetwork Strength FAQ
What's the trick in Neural Network Subnetwork Strength?+
Convert strong to +1 and weak to -1, then find the max-sum connected subtree containing each node. Do a downward DFS with clamped child sums, then a second top-down pass that reroots. Two linear passes, no repeated searches.
How hard is this Microsoft OA question really?+
Medium-hard. The tree DP is easy once you see it. The rerooting pass is where people stall. If you've done max-sum connected subtree for a single root, you're halfway there. The full answer needs the parent-side contribution.
Why does brute force fail here?+
Running a DFS from each of n neurons costs O(n^2). With a tree that can be large, that times out. Rerooting reuses the downward values so every neuron's answer comes from O(1) extra work.
How do I compute the parent's contribution when rerooting?+
For child c of parent p, take ans[p] and subtract max(0, down[c]), since p's best subnetwork may have included c's branch. The remainder is what p offers upward. Then ans[c] = down[c] + max(0, that remainder).
How do I prepare for this in 48 hours?+
Write the two-pass tree DP once from scratch. Practice the rerooting formula on a small star and a path. Use iterative traversal to avoid stack overflow. Test the sample by hand so the clamp logic is second nature.