Find where to cut cake into equal areas
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
### Cut a cake so both sides have equal area
A cake is made of **N** rectangular blocks arranged left-to-right in a straight line (no gaps, no overlap). Block `i` has:
- width `w_i`
- height `h_i`
All blocks share the same baseline; the cake’s top edge forms a step-like profile.
You want to make **one vertical cut** at position `x` (measured from the cake’s left edge) so that the **area to the left of the cut equals the area to the right**.
#### Input
- An integer `N`.
- Arrays `w[0..N-1]`, `h[0..N-1]`.
#### Output
- Return the cut position `x` as a real number such that:
- `0 <= x <= sum(w_i)`
- area(left of `x`) = total_area / 2
#### Notes
- If the cut falls inside a block, that block is split proportionally by width.
- Assume `total_area` is even in the sense that an exact cut position exists.
#### Complexity discussion
Explain your approach and its time/space complexity. (A common approach is to search which block contains the half-area point, then locate the exact offset inside that block.)
Quick Answer: This question evaluates geometric area computation, handling of piecewise-constant profiles formed by adjacent rectangles, and attention to numerical precision when partitioning contiguous segments.
Given adjacent rectangular blocks, return the x-coordinate of a vertical cut that splits total area in half.
Constraints
- heights are positive
- widths are positive
Examples
Input: ([2, 2], [1, 3])
Expected Output: 2.666667
Explanation: Cut falls in second block.
Input: ([4], [2])
Expected Output: 2.0
Explanation: Single rectangle.
Input: ([1, 3, 2], [2, 2, 2])
Expected Output: 3.0
Explanation: Uniform height.
Hints
- Scan cumulative area until the half-area point lies inside the current block.