Magicsheet logo

Count Number of Trapezoids II

Hard
12.5%
Updated 8/1/2025

Asked by 1 Company

Count Number of Trapezoids II

What is this problem about?

The "Count Number of Trapezoids II" coding problem is a high-level geometry and combinatorial challenge. Given a set of side lengths, you need to determine how many unique trapezoids can be formed. A trapezoid is a quadrilateral with at least one pair of parallel sides. The problem usually involves complex constraints on the side lengths to ensure they can actually form a closed figure.

Why is this asked in interviews?

Meta uses this "Hard" problem to test a candidate's ability to translate geometric constraints into mathematical inequalities and efficient counting logic. It requires a deep understanding of the Triangle Inequality and how it extends to quadrilaterals. It also tests the ability to use Hash Maps to group side lengths and optimize the O(n4)O(n^4) search space.

Algorithmic pattern used

The pattern involves Mathematical Constraints and Frequency Counting.

  1. A set of lengths (a,b,c,d)(a, b, c, d) can form a trapezoid if, assuming aa and bb are parallel, the remaining two sides cc and dd and the difference ab|a-b| satisfy the triangle inequality: ab<c+d|a-b| < c+d, c<ab+dc < |a-b|+d, and d<ab+cd < |a-b|+c.
  2. Use a Hash Map to store the frequencies of all available lengths.
  3. Iterate through pairs of potential parallel bases (a,b)(a, b) and then search for pairs (c,d)(c, d) that satisfy the quadrilateral existence conditions.

Example explanation

Suppose you have lengths [3, 3, 5, 10].

  • Try a=10,b=5a=10, b=5 as parallel sides. ab=5|a-b| = 5.
  • Remaining sides c=3,d=3c=3, d=3.
  • Check triangle inequality: 5<3+35 < 3+3 (True), 3<5+33 < 5+3 (True), 3<5+33 < 5+3 (True).
  • A trapezoid can be formed. If the lengths were [3, 3, 5, 12], ab=7|a-b|=7, and 7<3+37 < 3+3 is False, so no trapezoid exists.

Common mistakes candidates make

The most common mistake is failing to verify the existence condition for the non-parallel sides. Another is over-counting unique trapezoids if the side lengths are not distinct. Candidates also often struggle with the efficiency of the nested loops and need to use frequency maps or sorting to prune the search.

Interview preparation tip

Geometry problems often boil down to "Existence Conditions." For any polygon, the longest side must be shorter than the sum of all other sides. For trapezoids, this logic applies to the difference of the parallel bases and the two legs.

Similar Questions