Reported October 2019
Bloomberggreedy

Maximum Blocks in a Constrained Cave

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

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

Strip away the cave story and this Bloomberg OA question, reported in October 2019, is a plain matching problem. Blocks on one side, slots on the other, and a block fits a slot if its height is at most the capacity. You want the most pairs. It's greedy plus sorting, and it's easier than the wording suggests. If you blank under the timer, StealthCoder is the invisible safety net on your screen that reads the prompt and hands you the approach. But you should know the trick before you walk in.

The problem

Each block has unit width and fixed height. Each cave position has unit width and maximum height caveHeights[j]. Assign at most one block to each position; a block fits when its height is at most the position capacity.
Return the maximum number of blocks that can be placed. Blocks cannot rotate or stack.

Function
maximumCaveBlocks(blockHeights: int[], caveHeights: int[]) → int

Examples
Example 1
blockHeights = [4,2,3]
caveHeights = [3,5]
return = 2
Place height 2 in capacity 3 and height 3 or 4 in capacity 5.

Constraints
Each array contains at most 10^5 positive heights.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Sort both arrays ascending. Use two pointers, one on blocks and one on caves. If the current block fits the current cave, count it and advance both. If it doesn't fit, the cave is too small for this block and every bigger one, so skip the cave. Smallest block against smallest usable cave is the exchange argument that makes greedy correct. Check it on the example: blocks 2,3,4 and caves 3,5. Block 2 takes cave 3, block 3 takes cave 5, block 4 is left over. Answer is 2. The common pitfall is advancing the wrong pointer, or trying every pairing in O(n^2) with 10^5 elements per array. Sorting gives O(n log n), which is the target. Another trap is using strict less-than instead of at most. If the live OA catches you freezing on the pointer logic, StealthCoder is the hedge that gives you the working solution in real time.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Maximum Blocks in a Constrained Cave 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as assign cookies. If you have time before the OA, drill that.

⏵ The honest play

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

Bloomberg reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Blocks in a Constrained Cave FAQ

What's the trick in the Bloomberg Maximum Blocks in a Constrained Cave problem?+

Sort blocks and cave heights ascending, then walk two pointers. If the block fits the cave, count a match and move both. Otherwise skip the cave. The smallest block gets the smallest cave that holds it, which leaves bigger caves for bigger blocks.

How hard is this one really?+

Easy to medium. The story is dressed up, but it reduces to greedy matching after sorting. If you've seen assign-cookies style problems, you'll finish it fast. The main risk is overthinking it or writing a quadratic brute force.

What time complexity do I need with 10^5 elements?+

O(n log n) from sorting, then an O(n) two-pointer pass. Anything quadratic means around 10^10 operations at the max size, which will time out. Space is O(1) extra if you sort in place.

Does a block need to fit exactly, or is at most enough?+

At most is enough. A block fits when its height is less than or equal to the cave capacity. Use a less-than-or-equal comparison. Equal heights count as a valid match, so don't use strict less-than.

How do I prepare for this in 48 hours?+

Write the sorted two-pointer matching solution from scratch twice. Then test edge cases: empty-feeling inputs, all blocks too tall, all caves huge, and duplicate heights. Practice explaining why greedy is correct, since interviewers sometimes ask for the exchange argument.

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

OA at Bloomberg?
Invisible during screen share
Get it