Magicsheet logo

Simplified Fractions

Medium
25%
Updated 8/1/2025

Asked by 1 Company

Simplified Fractions

What is this problem about?

The "Simplified Fractions" interview question asks you to generate all possible "simplified" fractions between 0 and 1 (exclusive) where the denominator is less than or equal to a given integer n. A simplified fraction is one where the numerator and denominator have no common divisors other than 1—meaning they are coprime. This "Simplified Fractions coding problem" requires you to iterate through possible fractions and identify which ones cannot be reduced further.

Why is this asked in interviews?

Google and other tech companies use this question to test a candidate's understanding of basic number theory and their ability to implement mathematical concepts efficiently. It requires knowledge of the Greatest Common Divisor (GCD) and how to use it to check for coprimality. The problem also tests string formatting and nested loops, making it a well-rounded "MEDIUM" difficulty task that evaluates both logic and implementation skills.

Algorithmic pattern used

The core algorithmic pattern is "Math, Number Theory, and String interview pattern". You use a nested loop structure: the outer loop iterates through possible denominators d from 2 to n, and the inner loop iterates through possible numerators i from 1 to d-1. For each pair (i, d), you check if gcd(i, d) == 1. If it is, the fraction i/d is already in its simplest form and can be added to the result list as a string.

Example explanation

Let n = 4.

  • Denominator d = 2: Numerator i = 1. gcd(1, 2) = 1. Add "1/2".
  • Denominator d = 3:
    • i = 1: gcd(1, 3) = 1. Add "1/3".
    • i = 2: gcd(2, 3) = 1. Add "2/3".
  • Denominator d = 4:
    • i = 1: gcd(1, 4) = 1. Add "1/4".
    • i = 2: gcd(2, 4) = 2 (Not 1). Skip.
    • i = 3: gcd(3, 4) = 1. Add "3/4". Final List: ["1/2", "1/3", "2/3", "1/4", "3/4"].

Common mistakes candidates make

A common error is including fractions like 2/4 or 3/6, which are not simplified. This happens if the candidate forgets to use the GCD check. Another mistake is starting the numerator loop from 0 or including the case where the numerator equals the denominator, which would result in "0/d" or "1/1", neither of which are typically expected based on the problem's "between 0 and 1" constraint. Efficiency-wise, some candidates might try to use floating-point division to check for uniqueness, which can lead to precision errors; the GCD approach is much more robust.

Interview preparation tip

For any "Simplified Fractions interview question", knowing how to implement the Euclidean Algorithm for GCD is essential. It's a simple, recursive (or iterative) function that every developer should have in their toolkit: gcd(a, b) is gcd(b, a % b) with a base case of b == 0. Understanding this will help you solve many similar problems involving fractions, ratios, or grid-based movement.

Similar Questions