Unit Conversion II
Reported by candidates from Apple's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Apple's Unit Conversion II showed up in candidate reports in July 2026, and the first attempt usually dies on one detail: treating the answer as a plain integer ratio. It's a tree of units with multiplicative edges, and every query wants a fraction reduced modulo 10^9 + 7. The pattern is tree traversal plus modular inverses. If you've seen it, it's twenty lines. If you blank on the modular part mid-assessment, StealthCoder is the invisible safety net running on your screen while the proctor sees nothing.
The problem
There are n unit types indexed from 0 to n - 1. The array conversions has length n - 1. Each entry [sourceUnit, targetUnit, conversionFactor] means that one unit of sourceUnit is equivalent to conversionFactor units of targetUnit. Each entry [unitA, unitB] in queries asks how many units of unitB are equivalent to one unit of unitA. The exact answer can be written as a reduced fraction p / q. Return p * q^(-1) modulo 10^9 + 7, where q^(-1) is the multiplicative inverse of q modulo 10^9 + 7. Return one answer for every query in the same order. It is guaranteed that unit 0 can be converted uniquely into every other unit by combining forward conversions and inverse conversions. Function queryConversions(conversions: int[][], queries: int[][]) → int[] Examples Example 1 conversions = [[0,1,2],[0,2,6]] queries = [[1,2],[1,0]] return = [3,500000004] One unit of type 1 is half a unit of type 0. It is therefore equivalent to 3 units of type 2. The inverse of 2 modulo 10^9 + 7 is 500000004. Example 2 conversions = [[0,1,2],[0,2,6],[0,3,8],[2,4,2],[2,5,4],[3,6,3]] queries = [[1,2],[0,4],[6,5],[4,6],[6,1]] return = [3,12,1,2,83333334] Relative to unit 0, the conversion amounts are 2, 6, 8, 12, 24, and 24 for units 1 through 6. Dividing the destination amount by the source amount answers each query. The final query is 2 / 24 = 1 / 12, whose modular value is 83333334. Constraints 2 <= n <= 10^5 conversions.length = n - 1 Each conversion has the form [sourceUnit, targetUnit, conversionFactor]. 0 <= sourceUnit, targetUnit < n 1 <= conversionFactor <= 10^9 1 <= queries.length <= 10^5 Each query has the form [unitA, unitB] with both unit indices in [0, n - 1]. Unit 0 has exactly one conversion path to every unit when conversion edges are treated as bidirectional.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: n-1 edges and a guaranteed unique path from unit 0 means it's a tree. Run a BFS or iterative DFS from 0 and store val[u], the amount of unit u equal to one unit of 0, mod 10^9 + 7. Going along an edge s to t with factor f, val[t] = val[s] * f. Going backward, val[s] = val[t] * inverse(f). Then each query (a, b) is val[b] * inverse(val[a]) mod p. The pitfall that sinks first attempts is recursion depth at n = 10^5, plus computing floating point ratios or skipping the inverse. Use pow(x, p-2, p). Factors are at most 10^9 and below the prime, so they're never zero mod p. Precompute inverses once per node if you want, but per-query pow is fine at 10^5. If the modular algebra slips under pressure, StealthCoder can hand you the working solution live.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Unit Conversion II 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Apple's OA.
Apple 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.
Unit Conversion II FAQ
What's the trick in Apple's Unit Conversion II?+
Root the tree at unit 0 and compute each unit's value relative to 0 modulo 10^9 + 7. Every query then becomes val[b] times the modular inverse of val[a]. Edges are bidirectional, so going backward multiplies by the inverse of the factor instead.
How hard is this problem really?+
Medium. The graph part is a basic traversal. The difficulty is combining it with modular inverses and avoiding stack overflow on 10^5 nodes. If you know Fermat's little theorem for the inverse, it's straightforward.
Why do I need a modular inverse here?+
The true answer is a fraction p/q, and you must return p times q^-1 mod 10^9 + 7. Integer division would give wrong results. Compute the inverse with pow(q, 10^9+5, 10^9+7) since the modulus is prime.
Should I use DFS or BFS?+
BFS with a queue, or an iterative DFS. Recursive DFS can hit depth 10^5 on a chain-shaped tree and crash in many languages. Build an adjacency list storing both the factor and a direction flag for each edge.
How do I prepare for this in 48 hours?+
Practice one tree traversal that carries a running product, and write the modular inverse helper from memory. Test on Example 1 where the answer 500000004 is the inverse of 2. Check that you handle queries where unitA equals unitB, which should return 1.