KatInterview

Calculate Weight in a Nested Balanced Mobile System

Math & Bit ManipulationAuto-graded · 15 test cases
EasyAuto-gradedMath & Bit Manipulationalgebrarecursionmath

Last updated

Tests systematic application of balance constraints in a multi-level mobile where equal-arm bars divide weight evenly.

Calculate Weight in a Nested Balanced Mobile System

You are analyzing a hanging mobile where all shapes together have a known total weight. Each horizontal bar in the mobile has zero mass and features two arms of equal length. When arms are equal, balance requires that the total weight hanging from the left arm equals the total weight hanging from the right arm.

The mobile is structured as follows from top to bottom:

Top bar (Bar 1):

  • Left side: A complete sub-mobile (described below)
  • Right side: Other shapes (not detailed, but contribute to total balance)

The left sub-mobile has its own bar (Bar 2):

  • Left side: one orange octagon + one pink pentagon (hanging together)
  • Right side: Bar 3 (described below)

Bar 3:

  • Left side: Bar 4 (described below)
  • Right side: one gray octagon + one pink pentagon (hanging together)

Bar 4:

  • Left side: two purple hexagons (their combined weight hangs from this side)
  • Right side: one green triangle

Important details:

  • Every bar is massless and has equal-length arms on both sides
  • Balance means: left side total weight = right side total weight
  • The given total_weight is the sum of all shape weights across the entire mobile (bars contribute nothing)
  • When multiple shapes hang from the same side of a bar, their weights add together for that side's total

Determine the weight of the one green triangle.

Return the result as an integer representing pounds.

Constraints

  • total_weight is a positive integer
  • All bars have equal-length arms and zero mass
  • The mobile is in complete balance
  • The result is always a whole number

Examples

Example 1

Input: calculate_triangle_weight(160)

Output: 10

Hints

Hint 1

A bar with equal arms in balance means each side supports exactly half of the bar's total hanging weight.

Hint 2

Trace the path from the top bar down to the green triangle, applying the balance rule at each level.

Hint 3

The green triangle appears after following the left branch through multiple nested bars.