Count horizontal trapezoids formed by a set of points

Quick Overview

Given distinct points in the plane, count the quadrilaterals whose two horizontal sides lie on different rows and have different lengths, so parallelograms are excluded. It tests grouping by coordinate, combinatorial counting with a correction term, and care with counts too large for 32-bit integers.

Count horizontal trapezoids formed by a set of points

Company: Goldman Sachs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a set of `points` in a 2D Cartesian plane, count the number of horizontal trapezoids that can be formed using these points as vertices. A horizontal trapezoid is a quadrilateral with exactly two sides parallel to the x-axis, where those two sides have different lengths. Equivalently, it is formed by choosing two distinct points on one horizontal line (`y = y1`) and two distinct points on another horizontal line (`y = y2`), where the two horizontal segments have different lengths. If the lengths are equal, the figure is a parallelogram, which is not a trapezoid in the strict sense used here. Return the total number of distinct horizontal trapezoids. ### Function Signature ```python def count_horizontal_trapezoids(points: list[list[int]]) -> int: ``` ### Rules - `points[i] = [x, y]`. - The two horizontal sides must lie on different horizontal lines, and their lengths must differ. - Two trapezoids are different when their sets of four vertices differ. ### Constraints - `1 <= len(points) <= 1000` - `len(points[i]) == 2` - `-10^9 <= points[i][0], points[i][1] <= 10^9` - All points are distinct. ### Examples ```text Input: points = [[0,0],[2,0],[1,1],[3,1],[0,2],[4,2]] Output: 2 ``` Rows `y = 0` and `y = 1` each hold one segment of length 2, and row `y = 2` holds one of length 4. Pairing rows 0 and 1 gives equal lengths (a parallelogram), while pairing either of them with row 2 gives a trapezoid, so the answer is 2. ```text Input: points = [[0,0],[1,0],[0,1],[1,1]] Output: 0 ``` The four points form a unit square. Its two horizontal segments both have length 1, so they form a parallelogram and the answer is 0. ```text Input: points = [[0,0],[1,0],[3,0],[0,5],[2,5],[7,9]] Output: 2 ``` Row `y = 0` has segments of lengths 1, 2 and 3, row `y = 5` has one segment of length 2, and row `y = 9` has a single point, so it forms no segment. Of the three cross-row pairs, only the two length-2 segments make a parallelogram, which leaves 2.

Overview: Given distinct points in the plane, count the quadrilaterals whose two horizontal sides lie on different rows and have different lengths, so parallelograms are excluded. It tests grouping by coordinate, combinatorial counting with a correction term, and care with counts too large for 32-bit integers.

Goldman Sachs logo
Goldman Sachs
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a set of points in a 2D Cartesian plane, count the number of horizontal trapezoids that can be formed using these points as vertices.

A horizontal trapezoid is a quadrilateral with exactly two sides parallel to the x-axis, where those two sides have different lengths. Equivalently, it is formed by choosing two distinct points on one horizontal line (y = y1) and two distinct points on another horizontal line (y = y2), where the two horizontal segments have different lengths. If the lengths are equal, the figure is a parallelogram, which is not a trapezoid in the strict sense used here.

Return the total number of distinct horizontal trapezoids.

Function Signature

def count_horizontal_trapezoids(points: list[list[int]]) -> int:

Rules

  • points[i] = [x, y] .
  • The two horizontal sides must lie on different horizontal lines, and their lengths must differ.
  • Two trapezoids are different when their sets of four vertices differ.

Constraints

  • 1 <= len(points) <= 1000
  • len(points[i]) == 2
  • -10^9 <= points[i][0], points[i][1] <= 10^9
  • All points are distinct.

Examples

Input: points = [[0,0],[2,0],[1,1],[3,1],[0,2],[4,2]]
Output: 2

Rows y = 0 and y = 1 each hold one segment of length 2, and row y = 2 holds one of length 4. Pairing rows 0 and 1 gives equal lengths (a parallelogram), while pairing either of them with row 2 gives a trapezoid, so the answer is 2.

Input: points = [[0,0],[1,0],[0,1],[1,1]]
Output: 0

The four points form a unit square. Its two horizontal segments both have length 1, so they form a parallelogram and the answer is 0.

Input: points = [[0,0],[1,0],[3,0],[0,5],[2,5],[7,9]]
Output: 2

Row y = 0 has segments of lengths 1, 2 and 3, row y = 5 has one segment of length 2, and row y = 9 has a single point, so it forms no segment. Of the three cross-row pairs, only the two length-2 segments make a parallelogram, which leaves 2.