Reported December 2021
Airbnbgraph

Module Rebuild Costs

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

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

Airbnb's Module Rebuild Costs question showed up in a December 2021 report, and the detail that matters is the cost definition: it counts the module itself plus every module that depends on it, directly or transitively. So change N in Example 1 and you rebuild N, S, E and A, which is a cost of 4. It's a reverse-dependency graph problem on a DAG, with up to 10^4 modules and 2 * 10^5 edges. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and hands you the approach in real time. Read the rest first and you probably won't need it.

The problem

A codebase is split into modules connected by an acyclic dependency graph. Each string in dependencies is a comma-separated row. The first token names one module, and every later token names a module that it directly depends on.
Changing a module requires rebuilding that module and every module that depends on it, either directly or transitively. The cost of a module is the number of distinct modules rebuilt after changing it.
Return one string "module,cost" for every module, ordered lexicographically by module name.

Function
moduleRebuildCosts(dependencies: String[]) → String[]

Examples
Example 1
dependencies = ["A,E,N,S","S,H,N","E,N","H","N"]
return = ["A,1","E,2","H,3","N,4","S,2"]
Changing N rebuilds N, S, E, and A. Changing A rebuilds only A.
Example 2
dependencies = ["core","api,core","web,api,core"]
return = ["api,2","core,3","web,1"]
A change to core propagates to both api and web.

Constraints
1 <= dependencies.length <= 10^4.
Every module name is a unique non-empty ASCII identifier containing no comma.
Every referenced dependency has its own row in dependencies.
Each direct dependency appears at most once in a row.
The dependency graph is acyclic.
The total number of direct dependency edges is at most 2 * 10^5.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Flip the edges. The input says A depends on N, but you need N pointing to A, so build a reverse adjacency map from each dependency to its dependents. Then the cost of a module is the size of the set of nodes reachable from it in that reverse graph, counting itself. The pitfall is double counting. Diamond shapes mean the same dependent is reachable through several paths, so you need a visited set per start node, not a path counter. Running a DFS or BFS from every node costs O(V*(V+E)), which can be heavy at 2 * 10^5 edges. Summing child counts is wrong for the same diamond reason. Safer options are a per-node traversal with early optimization, or bitsets in topological order. Finally, sort names lexicographically and format each as module,cost. StealthCoder is the hedge if the DAG reachability logic slips under pressure.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Module Rebuild Costs 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Airbnb reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Module Rebuild Costs FAQ

What's the trick in Module Rebuild Costs?+

Reverse the dependency edges so each module points to the modules that depend on it. Then the cost is the count of distinct nodes reachable from it, including itself. Most wrong answers come from reading the edge direction backwards or counting paths instead of distinct modules.

Why can't I just add up my children's costs?+

Diamonds break it. If B and C both depend on N and A depends on both B and C, then A is reachable from N twice. Adding child counts counts A twice. You need distinct nodes, so use a visited set or a bitset union.

Will plain DFS from every node pass the constraints?+

It's the simplest correct approach and likely fine for typical cases, but worst case is O(V*(V+E)) with 10^4 nodes and 2 * 10^5 edges. If you worry about it, process in topological order and merge reachable sets with bitsets. Mention the tradeoff either way.

How do I parse the input rows quickly?+

Split each string on commas. The first token is the module, the rest are its dependencies. Register every module name, even ones with no dependencies, since they still need output rows. For each dependency d, add the module to dependents[d].

How do I prepare for this in 48 hours?+

Practice building a reverse graph and counting reachable nodes with DFS or BFS on a DAG. Then do one run with a diamond case and a single-node case. Check the output format, module,cost, and that you sort names lexicographically before returning.

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

OA at Airbnb?
Invisible during screen share
Get it