Find the most frequent fraction value when equivalent fractions count as equal
Company: SIG (Susquehanna)
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
Given two integer arrays `x` and `y` of the same length, where `x[i]/y[i]` represents a fraction, find the most frequently occurring fraction value.
Equivalent fractions must be treated as equal: `2/3`, `4/6` and `-2/-3` all have the same value. Return the most frequent value in its simplest form as a string `"numerator/denominator"`.
### Function Signature
```python
def most_frequent_fraction(x: list[int], y: list[int]) -> str:
```
### Rules
- Simplest form means the numerator and denominator share no common factor greater than 1 and the denominator is positive. A negative value carries its sign on the numerator, so `1/-2` is written `"-1/2"`.
- Any fraction with numerator 0 has the value zero, written `"0/1"`.
- If several values tie for the highest count, return the one whose first occurrence has the smallest index `i`.
### Constraints
- `1 <= len(x) == len(y) <= 10^5`
- `-10^9 <= x[i] <= 10^9`
- `-10^9 <= y[i] <= 10^9`
- `y[i] != 0`
### Examples
```text
Input: x = [2, 4, 1, 3, 6], y = [3, 6, 2, 9, 9]
Output: "2/3"
```
The fractions reduce to 2/3, 2/3, 1/2, 1/3 and 2/3, so 2/3 occurs three times.
```text
Input: x = [1, 2, 3, -1, -2], y = [2, 4, 6, -2, -4]
Output: "1/2"
```
All five fractions reduce to 1/2. A negative over a negative is positive, so -1/-2 and -2/-4 equal 1/2 as well.
```text
Input: x = [3, 1, 0, 2, 0], y = [-6, 3, 5, 6, -7]
Output: "1/3"
```
The values are -1/2, 1/3, 0, 1/3 and 0. Both 1/3 and 0 occur twice. 1/3 first appears at index 1 and 0 first appears at index 2, so the answer is 1/3.
Overview: Given two arrays that pair numerators with denominators, find the fraction value that occurs most often when equivalent fractions count as equal. It tests canonical forms with the greatest common divisor, sign and zero handling, hash map counting, and a deterministic tie-break.
You are given two integer arrays `x` and `y` of the same length. For every index `i`, the pair `x[i]/y[i]` represents a fraction. Find the fraction value that occurs most often.
Equivalent fractions are treated as equal: `2/3`, `4/6` and `-2/-3` all have the same value. Return the most frequent value in its simplest form as a string `"numerator/denominator"`.
### Rules
- Simplest form means the numerator and denominator share no common factor greater than 1 and the denominator is positive. A negative value carries its sign on the numerator, so `1/-2` is written `"-1/2"`.
- Any fraction with numerator 0 has the value zero, written `"0/1"`.
- The answer always has the `numerator/denominator` shape, so a whole-number value such as `6/3` is written `"2/1"`.
- If several values tie for the highest count, return the one whose first occurrence has the smallest index `i`.
### Constraints
- `1 <= len(x) == len(y) <= 10^5`
- `-10^9 <= x[i] <= 10^9`
- `-10^9 <= y[i] <= 10^9`
- `y[i] != 0`
Every input value and every reduced numerator and denominator has absolute value at most 10^9, so no value exceeds 2^31 - 1 and 32-bit integers (Java `int`, C++ `int`) are enough.
### Examples
**Example 1**
```text
Input: x = [2, 4, 1, 3, 6], y = [3, 6, 2, 9, 9]
Output: "2/3"
```
The fractions reduce to 2/3, 2/3, 1/2, 1/3 and 2/3, so 2/3 occurs three times.
**Example 2**
```text
Input: x = [3, 1, 0, 2, 0], y = [-6, 3, 5, 6, -7]
Output: "1/3"
```
The values are -1/2, 1/3, 0, 1/3 and 0. Both 1/3 and 0 occur twice. 1/3 first appears at index 1 and 0 first appears at index 2, so the answer is 1/3.
Constraints
- 1 <= len(x) == len(y) <= 10^5
- -10^9 <= x[i] <= 10^9
- -10^9 <= y[i] <= 10^9
- y[i] != 0
Examples
Input: ([2, 4, 1, 3, 6], [3, 6, 2, 9, 9])
Expected Output: '2/3'
Explanation: Source example 1: the pairs reduce to 2/3, 2/3, 1/2, 1/3 and 2/3, so 2/3 occurs three times.
Input: ([1, 2, 3, -1, -2], [2, 4, 6, -2, -4])
Expected Output: '1/2'
Explanation: Source example 2: every pair, including the two negative-over-negative pairs, reduces to the same value 1/2.
Hints
- Two pairs have the same value exactly when they reduce to the same lowest-terms pair with a positive denominator. Pick one representation per value before you count anything.
- Check how your representation handles 1/-2, -1/-2 and 0/-5: they must land on the same keys as -1/2, 1/2 and 0/1.
- For ties, compare where each value first appears in the input, not the moment its count reached the top during a scan.