Compute the total cost to build a part from its bill of materials tree

Quick Overview

Given parts with base costs and parent-child edges with quantities, compute the total cost of building the root part of a bill of materials tree. It tests building a tree from an edge list, bottom-up aggregation with compounding quantities, and handling deep trees without hitting recursion limits.

Compute the total cost to build a part from its bill of materials tree

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a tree structure representing a bill of materials, calculate the total cost to build the root item by summing the costs of all sub-components. Each node in the tree represents a part. Leaf parts have a direct base cost. An internal part costs the sum of the costs of all its children, each multiplied by the quantity of that child the part requires. You are given a list `parts` where `parts[i] = [id, base_cost]` and a list `edges` where `edges[i] = [parent_id, child_id, quantity]`. Return the total cost to build the root part, the part whose `id` is `0`. ### Function Signature ```python def calculate_total_cost(parts: list[list[int]], edges: list[list[int]]) -> int: pass ``` ### Rules - An edge `[parent_id, child_id, quantity]` means one unit of `parent_id` needs `quantity` units of `child_id`. - A part with no children is a leaf, and its cost is its `base_cost`. - A part with children costs the sum of `quantity * cost(child)` over its children. - Return the exact integer. Quantities multiply along each path from the root, so on a deep tree the result can be far larger than 64 bits. ### Constraints - `1 <= len(parts) <= 10^4` - `0 <= base_cost <= 10^4` - `1 <= quantity <= 100` - The edges form a valid tree rooted at part id `0`. - Only leaf parts have a non-zero `base_cost`. ### Examples ```text Input: parts = [[0,0],[1,0],[2,5],[3,3],[4,7]], edges = [[0,1,2],[0,2,1],[1,3,3],[1,4,1]] Output: 37 ``` Part 1 needs three of part 3 and one of part 4, so it costs `3*3 + 1*7 = 16`. The root needs two of part 1 and one of part 2, so it costs `2*16 + 1*5 = 37`. ```text Input: parts = [[0,0],[1,10],[2,20]], edges = [[0,1,1],[0,2,1]] Output: 30 ``` The root needs one of each leaf, so it costs `1*10 + 1*20 = 30`. ```text Input: parts = [[0,42]], edges = [] Output: 42 ``` The root has no children, so it is a leaf and costs its own base cost.

Overview: Given parts with base costs and parent-child edges with quantities, compute the total cost of building the root part of a bill of materials tree. It tests building a tree from an edge list, bottom-up aggregation with compounding quantities, and handling deep trees without hitting recursion limits.

Stripe logo
Stripe
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a tree structure representing a bill of materials, calculate the total cost to build the root item by summing the costs of all sub-components.

Each node in the tree represents a part. Leaf parts have a direct base cost. An internal part costs the sum of the costs of all its children, each multiplied by the quantity of that child the part requires.

You are given a list parts where parts[i] = [id, base_cost] and a list edges where edges[i] = [parent_id, child_id, quantity]. Return the total cost to build the root part, the part whose id is 0.

Function Signature

def calculate_total_cost(parts: list[list[int]], edges: list[list[int]]) -> int:
    pass

Rules

  • An edge [parent_id, child_id, quantity] means one unit of parent_id needs quantity units of child_id .
  • A part with no children is a leaf, and its cost is its base_cost .
  • A part with children costs the sum of quantity * cost(child) over its children.
  • Return the exact integer. Quantities multiply along each path from the root, so on a deep tree the result can be far larger than 64 bits.

Constraints

  • 1 <= len(parts) <= 10^4
  • 0 <= base_cost <= 10^4
  • 1 <= quantity <= 100
  • The edges form a valid tree rooted at part id 0 .
  • Only leaf parts have a non-zero base_cost .

Examples

Input: parts = [[0,0],[1,0],[2,5],[3,3],[4,7]], edges = [[0,1,2],[0,2,1],[1,3,3],[1,4,1]]
Output: 37

Part 1 needs three of part 3 and one of part 4, so it costs 3*3 + 1*7 = 16. The root needs two of part 1 and one of part 2, so it costs 2*16 + 1*5 = 37.

Input: parts = [[0,0],[1,10],[2,20]], edges = [[0,1,1],[0,2,1]]
Output: 30

The root needs one of each leaf, so it costs 1*10 + 1*20 = 30.

Input: parts = [[0,42]], edges = []
Output: 42

The root has no children, so it is a leaf and costs its own base cost.