Reported March 2026
Two Sigmadynamic programming

Sewer Drainage Partition

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

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

Two Sigma asked this in March 2026. You've got a rooted tree where water flows upward, and you need to cut exactly one edge to split it into two subtrees with flows as balanced as possible. This is a tree DP problem dressed up in plumbing language. The trick: compute subtree sums bottom-up, then for each possible cut, the difference is the absolute gap between one partition and the total minus that partition. StealthCoder will spot the pattern instantly if you freeze on the tree traversal.

The problem

A sewer drainage system forms a rooted tree. Water enters at n nodes numbered from 0 to n - 1 and flows upward to the root 0. The tree is described by parent, where parent[i] = j means water flows from node i to its direct parent j. The root has parent[0] = -1. input[i] is the amount of water entering directly at node i. The total flow through a node is its own direct input plus the total flows of all of its children. Cut exactly one branch so the tree is split into two subtrees. Return the minimum possible absolute difference between the total flows of the two resulting parts. Function Description Complete drainagePartition. drainagePartition has the following parameters: Returns: int minimum absolute difference after cutting one branch. Cut the edge between nodes 1 and 0. One component has nodes {0,2,5} with total flow 4, and the other has nodes {1,3,4} with total flow 4. The difference is 0. Cut the edge between nodes 1 and 2. The two resulting parts have total flows 5 and 7, so the minimum difference is 2.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The core pattern is dynamic programming on trees. First, run a DFS to compute the total flow in each subtree (node's input plus all descendants' flows). The total flow in the entire tree is fixed. When you cut the edge above node i, one partition has flow equal to subtree[i], and the other has flow equal to total minus subtree[i]. The answer is the minimum absolute difference across all possible cuts. The pitfall: forgetting to exclude the root from being cut (you can't cut the edge above the root). Build a parent map, do a post-order traversal to sum flows, then iterate all non-root nodes and track the minimum difference. StealthCoder's hedge here is catching the indexing logic if you blank on the tree structure.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Sewer Drainage Partition 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as partition equal subset sum. If you have time before the OA, drill that.

⏵ The honest play

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

Two Sigma reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Sewer Drainage Partition FAQ

Do I need to build an adjacency list or can I work with the parent array directly?+

You can work with the parent array for the upward direction, but you'll need to build a children map (or adjacency list) to compute subtree sums. Use the parent array to build a dictionary of node to children. Then DFS from the root downward to accumulate flows.

What's the trick to avoid checking all O(n^2) cuts?+

There's no trick: you must check all n-1 possible cuts (every edge except root). But computing all subtree sums in one DFS is O(n), then iterating cuts is O(n), so total is O(n). The key is doing the subtree sum calculation once, not recomputing for each cut.

How do I handle the constraint that I can't cut above the root?+

Simple: when iterating candidate cuts, skip node 0 (the root). For all other nodes, cutting the edge between that node and its parent is valid. Only n-1 edges are cuttable, which is what you want.

Is this really just a partition problem?+

Yes. The total flow is constant. Cutting an edge creates two subtrees with flows that sum to the total. Minimize the absolute difference between them. Once you have all subtree sums, it's a one-pass scan to find the minimum gap.

What's a common bug candidates hit?+

Forgetting to initialize the children map, so leaf nodes don't contribute their input to the total. Also, off-by-one errors when mapping node indices to subtree sums. Trace through the example by hand first.

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

OA at Two Sigma?
Invisible during screen share
Get it