Magicsheet logo

Maximum Area of Longest Diagonal Rectangle

Easy
77.6%
Updated 6/1/2025

Asked by 4 Companies

Topics

Maximum Area of Longest Diagonal Rectangle

What is this problem about?

You are given a 2D array where each element is an array [length, width] representing the dimensions of a rectangle. You need to calculate the length of the diagonal for each rectangle. Your goal is to find the rectangle that has the strictly longest diagonal. If there is a tie (multiple rectangles have the exact same longest diagonal), you must return the maximum area among those tied rectangles.

Why is this asked in interviews?

This is an entry-level Array Traversal and Math problem. It tests basic geometric formulas (Pythagorean theorem) and multi-variable state tracking. Interviewers use it as a warm-up to ensure candidates can write conditional logic that handles tie-breakers without writing messy, convoluted if-else trees.

Algorithmic pattern used

This problem follows a simple Linear Scan (Single Pass) pattern. You iterate through the list of rectangles, maintaining two global tracking variables: max_diagonal and max_area_of_longest_diagonal. For each rectangle, calculate its diagonal squared (L2+W2L^2 + W^2).

  • If this diagonal squared is strictly greater than max_diagonal, update max_diagonal and completely overwrite max_area with the current rectangle's area.
  • If this diagonal squared is exactly equal to max_diagonal, you update max_area to be the max(max_area, current_area).

Example explanation

Rectangles: [[9, 3], [8, 6]]

  • Rectangle 1: [9, 3].
    • Diagonal squared = 92+32=81+9=909^2 + 3^2 = 81 + 9 = 90.
    • Area = 9×3=279 \times 3 = 27.
    • This is the first one, so max_diag = 90, max_area = 27.
  • Rectangle 2: [8, 6].
    • Diagonal squared = 82+62=64+36=1008^2 + 6^2 = 64 + 36 = 100.
    • Area = 8×6=488 \times 6 = 48.
    • Since 100>90100 > 90, this is the new longest diagonal. We overwrite state.
    • max_diag = 100, max_area = 48. Return 48.

If another rectangle had diag = 100 but area = 50, the equality condition would trigger and max_area would bump up to 50.

Common mistakes candidates make

A common mistake is using Math.sqrt() to calculate the exact diagonal length. Floating-point square roots suffer from precision loss. If two diagonals are extremely close in value, a float might evaluate them as equal when they aren't. Because the square root function is strictly increasing, A>BA > B implies A>B\sqrt{A} > \sqrt{B}. Therefore, you should always compare the squared diagonal lengths (L2+W2L^2 + W^2) using exact integers to ensure 100% accuracy and faster computation.

Interview preparation tip

When an interview question involves geometric distances, distances between points, or diagonals, avoid Math.sqrt unless the prompt explicitly asks for a float return type. Comparing squared distances using integers is an industry-standard optimization that shows the interviewer you care about both performance and type safety.

Similar Questions