Champagne Tower - Problem
Champagne Tower Problem

Imagine you're at an elegant celebration where champagne glasses are arranged in a beautiful pyramid formation. The setup is breathtaking: 1 glass at the top, 2 glasses in the second row, 3 glasses in the third row, and so on, creating a perfect triangular tower up to 100 rows.

๐Ÿฅ‚ The Physics: When champagne is poured into the topmost glass, it follows the laws of gravity and overflow. Once a glass becomes completely full (holds exactly 1 cup), any additional champagne splits equally between the two glasses directly below it - one to the left diagonal and one to the right diagonal.

๐ŸŽฏ Your Mission: Given that poured cups of champagne are poured into the top glass, determine how full the glass at position (queryRow, queryGlass) is. The result should be a decimal between 0.0 (empty) and 1.0 (completely full).

Input: Three integers - poured (amount of champagne), queryRow and queryGlass (both 0-indexed)
Output: A decimal representing how full the specified glass is (clamped between 0.0 and 1.0)

Input & Output

example_1.py โ€” Basic Overflow
$ Input: poured = 1, query_row = 1, query_glass = 1
โ€บ Output: 0.00000
๐Ÿ’ก Note: With only 1 cup poured, the top glass becomes full (1.0) but has no overflow. The glasses in row 1 remain empty.
example_2.py โ€” Equal Distribution
$ Input: poured = 2, query_row = 1, query_glass = 1
โ€บ Output: 0.50000
๐Ÿ’ก Note: The top glass holds 1.0, and the remaining 1.0 cup splits equally between the two glasses in row 1. Each gets 0.5.
example_3.py โ€” Multiple Level Overflow
$ Input: poured = 100000009, query_row = 33, query_glass = 17
โ€บ Output: 1.00000
๐Ÿ’ก Note: With an enormous amount of champagne, virtually all glasses in the pyramid become completely full (1.0).

Constraints

  • 0 โ‰ค poured โ‰ค 109
  • 0 โ‰ค query_row, query_glass โ‰ค 99
  • query_glass โ‰ค query_row
  • All inputs are integers except poured can be float
  • Answer within 10-5 of actual answer is accepted

Visualization

Tap to expand
๐Ÿฅ‚ Champagne Tower Flow Simulation4.01.51.50.251.00.25๐ŸŒŠ Flow Direction: Top โ†’ Bottom, Level by Levelโฑ๏ธ Time Complexity: O(Rยฒ) | ๐Ÿ’พ Space: O(R)Full/Overflowing GlassPartially Filled Glass
Understanding the Visualization
1
๐Ÿพ Initial Pour
All champagne starts in the topmost glass
2
๐Ÿ’ง Overflow Check
If a glass holds more than 1.0, it overflows
3
โš–๏ธ Equal Split
Excess champagne splits equally between two glasses below
4
๐Ÿ”„ Level Processing
Process entire level before moving to next
5
๐ŸŽฏ Target Reached
Return the amount in our target glass
Key Takeaway
๐ŸŽฏ Key Insight: Model the natural physics - champagne flows from top to bottom, level by level. Each glass holds at most 1.0 units, and excess splits equally between its two children below. Process complete levels sequentially for optimal efficiency.
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18 Apple 15
42.6K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen