PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates geometric area computation, handling of piecewise-constant profiles formed by adjacent rectangles, and attention to numerical precision when partitioning contiguous segments.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

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

  1. Scan cumulative area until the half-area point lies inside the current block.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Infection Spread on a Grid (Cellular Automaton) - Google (hard)
  • Boolean Expression Tree with Leaf Flips - Google (medium)
  • Streaming Points: Remove Any Pair Within a Distance - Google (medium)
  • Most Active Users in a Live Communication Stream - Google (medium)
  • Solve Rooms and Top-K Streams - Google (medium)