Magicsheet logo

Number Of Rectangles That Can Form The Largest Square

Easy
100%
Updated 8/1/2025

Asked by 1 Company

Topics

Number Of Rectangles That Can Form The Largest Square

What is this problem about?

The Number Of Rectangles That Can Form The Largest Square problem gives you a list of rectangles as [length, width] pairs. A rectangle can form a square of side k if both its dimensions ≥ k. Find the maximum k achievable by any rectangle, then count how many rectangles can achieve this maximum. This easy coding problem requires finding the global maximum min-dimension and counting.

Why is this asked in interviews?

Allincall asks this simple array problem to test clean max-finding and counting logic. It validates that candidates can iterate an array, track a running maximum, and count elements matching that maximum. The array interview pattern is demonstrated in its most basic form.

Algorithmic pattern used

Two-pass (or single-pass). For each rectangle, compute min(l, w) — the maximum square it can form. Find the global maximum of these values. Count rectangles where min(l, w) == global_max. Single-pass: track max_side and count; reset count when a new max is found.

Example explanation

Rectangles: [[5,8],[3,9],[5,12],[16,5]]. min-dimensions: [5,3,5,5]. Max = 5. Count with min=5: [5,8]✓, [5,12]✓, [16,5]✓. Count = 3.

Common mistakes candidates make

  • Finding max length and max width separately instead of max(min(l,w)).
  • Not resetting count when a new global maximum is found.
  • Using two passes when a single pass with reset logic suffices.
  • Off-by-one in rectangle indexing.

Interview preparation tip

Problems requiring "find max and count occurrences of max" are cleanly solved in one pass: maintain best and count. When min(l,w) > best, update best = min(l,w) and reset count = 1. When min(l,w) == best, increment count. Otherwise ignore. Practice this "running max with count" template — it appears in many easy counting problems and can always be done in a single O(n) pass.

Similar Questions