FastPrepHorizontal Line Bisecting Total Square Area

Horizontal Line Bisecting Total Square Area

Google logoGoogle● HardFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Each axis-aligned square is [topLeftX,topLeftY,side] and spans vertically from topLeftY-side to topLeftY.

Return the y-coordinate of a horizontal line for which the sum of square areas below the line equals the sum above it. Overlapping areas are counted once per square. Return the smallest valid coordinate when an interval of answers exists.

Function

horizontalBisector(squares: int[][]) → double

Examples

Example 1

squares = [[1,2,5],[2,4,10]]return = -0.8333333333333335

A sweep accounts for the changing total horizontal width; a weighted-center average is not generally an area bisector.

Example 2

squares = [[0,2,2]]return = 1

A single square is bisected halfway between y=0 and y=2.

Example 3

squares = []return = 0

The empty input uses coordinate zero.

Constraints

  • 0 <= squares.length <= 100000.
  • 1 <= side <= 10^9.
  • An absolute error of 10^-6 is accepted.

More Google problems

See Google hiring insights
public double horizontalBisector(int[][] squares) {
    // write your code here
}
squares[[1,2,5],[2,4,10]]
expected-0.8333333333333335
Checking account…