Prime Tree City Labelings
Reported by candidates from Rippling's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Rippling reported this one in July 2026, and it looks scarier than it is. Strip the story and it's a tree DP over 25 states. Each city gets one of the 25 primes up to 100, and every road forbids pairs whose sum is prime. If you've got an OA coming up, expect n up to 200000, so recursion depth and speed both matter. The real work is spotting that the forbidden pairs have structure. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the pattern below should get you most of the way there.
The problem
Hackerland contains n cities numbered from 1 to n. Its n - 1 roads form a tree, where road i connects edgeFrom[i] and edgeTo[i]. Assign one prime number from 2 through 100, inclusive, to every city. Prime values may be reused. For every road, the sum of the prime values assigned to its two endpoints must not be prime. Return the number of valid assignments modulo 10^9 + 7. Function countPrimeLabelings(n: int, edgeFrom: int[], edgeTo: int[]) → int Examples Example 1 n = 2 edgeFrom = [1] edgeTo = [2] return = 609 Among all ordered assignments of primes from 2 through 100 to the two cities, 609 have a non-prime endpoint sum. Example 2 n = 1 edgeFrom = [] edgeTo = [] return = 25 There are 25 primes from 2 through 100, and a one-city tree has no road constraint. Constraints 1 <= n <= 200000 edgeFrom.length = edgeTo.length = n - 1 The roads form a tree.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Root the tree anywhere. Let dp[v][p] be the number of valid labelings of v's subtree when v has prime p. For each child c, multiply by the sum of dp[c][q] over all q where p+q is not prime. That's a 25x25 compatibility check per edge, about 625 operations, which is fine for 200000 nodes. The parity trick: two odd primes sum to an even number above 2, so it's never prime. Only pairs involving 2 can produce a prime sum, since 2+2=4 isn't prime. So the compatibility matrix is almost all ones, and you can speed it up with a total sum minus a few bad terms. The pitfall is recursive DFS blowing the stack at n = 200000. Use an iterative order with a parent array. Take everything modulo 10^9 + 7. If you freeze on the iterative setup, StealthCoder is the hedge during the live OA.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Prime Tree City Labelings 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Rippling's OA.
Rippling reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Prime Tree City Labelings FAQ
What's the trick in Prime Tree City Labelings?+
It's a tree DP with 25 states per node, one per prime up to 100. For each edge, a child's contribution to a parent value p is the sum of its dp values over primes q where p+q is not prime. Precompute that compatibility once, then combine children by multiplication.
How hard is this Rippling OA question really?+
Medium. The tree DP is standard, and the small state space keeps it cheap. The difficulty is noticing the state is just 25 primes and handling n = 200000 without recursion errors. Once you see that, it's about 40 lines of code.
Why do the examples return 609 and 25?+
With one city there's no road, so any of the 25 primes works, giving 25. With two cities there are 25 x 25 = 625 ordered pairs. 16 of them have a prime sum, leaving 609. Use this as your first sanity check.
Will recursion work for n up to 200000?+
Not safely. A path-shaped tree gives depth 200000, which can overflow the call stack in many languages. Build an adjacency list, compute a BFS or DFS order iteratively with parents, then process nodes in reverse order to fold children into parents.
How do I prepare for this in 48 hours?+
Write rooted tree DP with iterative traversal until it's automatic. Practice a state-per-node DP where you multiply child sums. Then code this one: sieve primes to 100, build the 25x25 allowed table, run the reverse-order DP, and test against 609 and 25.