Rocket Component Cost
Reported by candidates from SpaceX's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The SpaceX Rocket Component Cost question, reported in August 2025, sounds like aerospace but it's a bill-of-materials roll-up. Every part has a direct cost and a list of subparts with quantities. You total it up for one target part. Under the story it's a DAG traversal with memoization, and that's the whole game. If you've got the OA coming in a day or two, learn the shape now. And if your head goes blank mid-assessment, StealthCoder runs invisibly on your desktop and can hand you the solution live.
The problem
Rocket Component Cost
Write code to calculate the total cost of building a rocket. You are provided a map from each part to its cost and another map from each part to the required subparts and their quantities.
The reported dependency representation has the form {part: [[required_part_1, amount_1], [required_part_2, amount_2],...]}. The source notes that a solution can cache computed costs while traversing component dependencies with DFS or BFS.
Practice Contract
For this exercise, assume parts[i] has direct unit cost directCosts[i]. The aligned rows requiredParts[i] and requiredAmounts[i] list the immediate subparts needed to build one unit of parts[i].
The total cost of one part is its direct cost plus, for every required subpart, the required quantity multiplied by that subpart's total cost. Return the total cost of one targetPart.
Part names are unique, every referenced subpart exists, and the dependency graph is acyclic. Shared subassemblies may be required from multiple places, so avoid recomputing their total costs.
Function
calculateRocketCost(parts: String[], directCosts: long[], requiredParts: String[][], requiredAmounts: int[][], targetPart: String) → long
Examples
Example 1
parts = ["rocket","engine","tank","metal"]
directCosts = [100,20,1000,5]
requiredParts = [["engine","tank"],["metal"],[],[]]
requiredAmounts = [[2,1],[100],[],[]]
targetPart = "rocket"
return = 2140
One engine costs 20 + 100 × 5 = 520. The rocket costs 100 + 2 × 520 + 1 × 1000 = 2140.
Example 2
parts = ["rocket"]
directCosts = [42]
requiredParts = [[]]
requiredAmounts = [[]]
targetPart = "rocket"
return = 42
The target has no required subparts, so its total cost is its direct cost.
Example 3
parts = ["rocket","stage","engine","bolt"]
directCosts = [0,0,0,2]
requiredParts = [["stage","engine"],["engine","bolt"],["bolt"],[]]
requiredAmounts = [[2,1],[1,10],[50],[]]
targetPart = "rocket"
return = 340
An engine costs 100, a stage costs 100 + 20 = 120, and the rocket costs 2 × 120 + 100 = 340. Memoization computes the shared engine assembly once.
Constraints
1 ≤ parts.length ≤ 10,000
parts.length == directCosts.length == requiredParts.length == requiredAmounts.length
Part names are unique, non-empty, and every required-part name appears in parts.
For every i, requiredParts[i].length == requiredAmounts[i].length.
0 ≤ directCosts[i] ≤ 10^9 and 1 ≤ requiredAmounts[i][j] ≤ 10^6.
The dependency graph is acyclic.
The answer fits in a signed 64-bit integer.Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's the reduction: cost(part) = directCost + sum(amount * cost(subpart)). Build a hash map from part name to index, then run DFS with a memo map. Check the memo first, compute recursively, store the result. Shared subassemblies like the engine in Example 3 get computed once, which is the entire point of the problem. The pitfall is skipping the cache. A DAG with shared nodes can blow up exponentially without it. Second pitfall is overflow. Use 64-bit longs, since amounts reach 10^6 and costs reach 10^9. Recursion depth can hit 10,000 on a long chain, so consider an iterative topological order if your language is stingy with stack. Only compute from the target, not every part. If you freeze on the recursion or the indexing during the live OA, StealthCoder is the hedge that gets you unstuck without the proctor seeing anything.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Rocket Component Cost 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 SpaceX's OA.
SpaceX 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.
Rocket Component Cost FAQ
How hard is the SpaceX Rocket Component Cost problem really?+
Easy to medium. The logic is one recurrence: direct cost plus quantity times subpart cost. The difficulty is noticing you need memoization, and handling 64-bit math. If you've written DFS with a cache before, it's a 15 minute problem.
What's the trick to solving it?+
Memoized DFS over the dependency graph. Map part names to indices, then compute total cost recursively and store each result. Shared subassemblies get reused instead of recomputed. Without the cache, repeated shared parts can make runtime explode.
Do I need to worry about integer overflow?+
Yes. Direct costs go up to 10^9 and quantities up to 10^6, and the answer is guaranteed to fit in signed 64-bit. Use long in Java or C++. Python handles it automatically. Don't use 32-bit ints for the memo values.
Should I use DFS or BFS?+
DFS with memoization is simplest, since the cost of a part depends on its children. BFS works if you do a topological order and process from leaves up. For 10,000 parts, watch recursion depth and go iterative if your language limits the stack.
How do I prepare for this in 48 hours?+
Write the memoized DFS from scratch twice, once recursive and once with topological order. Test against the three examples, especially the shared engine case. Also practice building the name-to-index map from the aligned arrays, since that setup is where small bugs creep in.