Count Trapped Unit Balls
Updated
Problem statement
Adjacent towers have heights given by heights. Count how many balls can remain trapped between them. Each tower is one unit wide, and each ball has diameter one unit, equal to the tower width.
For this exercise, use a discrete column model: balls stack vertically in unit-height layers above a column’s tower. Their centers stay aligned with that unit column; staggered or diagonal packing is excluded. A stack is retained only below both a left boundary and a right boundary. Its top cannot exceed the shorter of the tallest boundaries on those two sides. Unbounded exterior space holds no trapped balls.
Return the total number of balls in a fully filled retained arrangement under that model. With no columns or no bounded cavity, return zero. This is a trapping-rainwater variation with balls.
Function
countTrappedBalls(heights: int[]) → intExamples
Example 1
heights = [3,0,2,0,4]return = 7The three interior columns hold vertical stacks of 3, 1 and 3 unit balls. The boundary columns hold none, giving 7 balls in total.
Example 2
heights = [0,1,0]return = 0Each low exterior column is open on one side. No bounded cavity retains a ball.
Constraints
- For this exercise, assume
0 <= heights.length <= 1000and0 <= heights[i] <= 10000. - Tower widths and ball diameters are one unit. Heights and vertical stack capacities are integers.
- Balls stay in their original unit columns and stack vertically without staggered packing.
More JP Morgan Chase Online Assessment questions
- Find Total CostOA · Seen Oct 2026
- Minimum Meeting RoomsOA · Seen Oct 2026
- Count Dropped RequestsOA · Seen Oct 2026
- Maximize Minimum Deployment DifficultyOA · Seen Oct 2026
- Task SchedulingOA · Seen Oct 2026
- Substring RemovalOA · Seen Oct 2026
- Maximum Game Board ScoreOA · Seen Sep 2026
- Minimum Digit Changes Between String HalvesOA · Seen Sep 2026
- Same Substring Within BudgetOA · Seen Sep 2026
- Invert Ad Visibility BitsOA · Seen Aug 2026
Similar questions at other companies
- Count Subarrays With Product Equal to KSalesforce · OASalesforce interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Usable Water with Contaminated PillarsInfosys · OAInfosys interview questions
- Latest Arrival to Catch a BusAutodesk · OAAutodesk interview questions
- Minimum Last-Element RelocationsChubb · OAChubb interview questions
- Generating Login CodesSnowflake · OASnowflake interview questions
- Paint the CeilingWeRide · OAWeRide interview questions
- Rotate an Array In PlaceAdobe · OA · PHONE SCREENAdobe interview questions
- Minimum Adjacent Merges to Form a PalindromeArista Networks · OAArista Networks interview questions
- As-Of JoinHeadlands Technologies · OA · ONSITE INTERVIEWHeadlands Technologies interview questions
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)