Quick Overview

Count distinct axis-aligned squares whose full sides are covered by horizontal and vertical integer-coordinate segments. Normalize and merge collinear coverage, avoid duplicate geometric squares, support overlapping segments and negative coordinates, and index interval coverage efficiently.

Count Squares Formed by Segments

Company: Waymo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

# Count Squares Formed by Segments You are given axis-aligned line segments with integer coordinates. Each segment is represented by two endpoints. One input list contains only vertical segments and another contains only horizontal segments. Count the distinct axis-aligned squares whose four complete sides are covered by the supplied segments. A valid square is defined by `x1 < x2` and `y1 < y2` such that `x2 - x1 == y2 - y1`, both vertical sides cover the full range `[y1, y2]`, and both horizontal sides cover the full range `[x1, x2]`. Implement: ```python def count_squares( vertical: list[tuple[tuple[int, int], tuple[int, int]]], horizontal: list[tuple[tuple[int, int], tuple[int, int]]], ) -> int: ... ``` ## Constraints and Clarifications - Endpoint order is arbitrary; normalize every segment. - Zero-length segments do not form a side. - Collinear segments may overlap or touch. Their union can cover a square side, and the same geometric square is counted once regardless of how many source segments cover it. - Intersections at endpoints count. - Coordinates may be negative. - Do not assume the drawing is a complete grid. - Discuss a baseline approach and how you would index candidate counterpart segments to avoid blindly testing every set of four input segments. ## Hints - Normalize collinear coverage before counting to prevent duplicates. - A pair of parallel sides determines the only possible side length and the required positions of the other pair. - Consider indexing intervals by their fixed coordinate and supporting fast coverage queries on the varying coordinate.

Overview: Count distinct axis-aligned squares whose full sides are covered by horizontal and vertical integer-coordinate segments. Normalize and merge collinear coverage, avoid duplicate geometric squares, support overlapping segments and negative coordinates, and index interval coverage efficiently.

Count distinct axis-aligned squares whose four full sides are covered by unions of the supplied vertical and horizontal segments.

Constraints

  • Segments are axis-aligned with integer coordinates
  • Endpoint order is arbitrary
  • Overlapping or touching segments may jointly cover a side

Examples

Input: {'vertical': [], 'horizontal': []}

Expected Output: 0

Explanation: No segments form no square.

Input: {'vertical': [((0, 0), (0, 1)), ((1, 0), (1, 1))], 'horizontal': [((0, 0), (1, 0)), ((0, 1), (1, 1))]}

Expected Output: 1

Explanation: Four unit sides form one square.

Hints

  1. Merge collinear coverage before counting.
  2. A pair of vertical coordinates fixes the side length and the required second horizontal level.

Loading coding console...

Show the approach

Approach

Merging coverage prevents duplicates and lets each side be tested as one contained interval. Every vertical coordinate pair fixes a positive side length; each horizontal level then determines at most one square to verify.

Time complexity:
O(S log S + V^2 H I) for the baseline indexed coverage checks
Space complexity:
O(S)