Quick 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.

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

  1. 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.
  2. 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.
  3. For ties, compare where each value first appears in the input, not the moment its count reached the top during a scan.

Most Frequent Fraction Value in Lowest Terms

easySIG (Susquehanna)Software Engineer

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

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

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.

Examples

Example 1
Input
([2, 4, 1, 3, 6], [3, 6, 2, 9, 9])
Output
'2/3'
Notes
Source example 1: the pairs reduce to 2/3, 2/3, 1/2, 1/3 and 2/3, so 2/3 occurs three times.
Example 2
Input
([1, 2, 3, -1, -2], [2, 4, 6, -2, -4])
Output
'1/2'
Notes
Source example 2: every pair, including the two negative-over-negative pairs, reduces to the same value 1/2.

Constraints

  • •1 <= len(x) == len(y) <= 10^5
  • •-10^9 <= x[i] <= 10^9
  • •-10^9 <= y[i] <= 10^9
  • •y[i] != 0

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def most_frequent_fraction(x: list[int], y: list[int]) -> str:
Sample call
most_frequent_fraction([2, 4, 1, 3, 6], [3, 6, 2, 9, 9])
Language
Case 1
Input
([2, 4, 1, 3, 6], [3, 6, 2, 9, 9])
Expected
'2/3'
Case 2
Input
([1, 2, 3, -1, -2], [2, 4, 6, -2, -4])
Expected
'1/2'

Browse More Questions

Show the approach

Approach

Turn every pair into one canonical key, count the keys, then pick the winner.

  1. Canonical key. For a pair (a, b) with a != 0, divide both parts by g = gcd(|a|, |b|) using Euclid's algorithm, then negate both parts if the denominator is negative. If a == 0 the key is (0, 1). Every rational number has exactly one representation in lowest terms with a positive denominator, so two pairs have the same value exactly when their keys are equal.
  2. Counting with first occurrence. The Python reference keeps a dictionary of counts plus a list of keys in the order they first appear. Walking that list and replacing the current best only on a strictly larger count leaves, among the keys with the highest count, the one that appeared earliest. The JavaScript, Java and C++ references sort the indices by (numerator, denominator, index), so each value forms one contiguous group whose first element is its first occurrence; they keep the largest group and, on equal size, the group with the smaller first index. Both procedures return the same answer.
  3. Output. Join the numerator and denominator with '/'.

Edge cases: a single pair; zero over a positive or negative denominator (always 0/1); a negative denominator or both parts negative; whole numbers such as 6/3, written 2/1; values at +/-10^9, where every intermediate stays within a 32-bit int because |a|, |b| <= 10^9; and a tie in which a value that first appears later reaches the top count earlier during the scan, which must still lose to the value that first appears earlier.

Time complexity:
O(n log n + n log V), where V = 10^9 bounds the absolute values (the Python reference alone is O(n log V))
Space complexity:
O(n)