Implement a normalized S4 Rational number class in R

Quick Overview

Implement an S4 Rational class in R with a validity method, a normalizing constructor, and generics for the accessors, addition, multiplication and conversion to double. It tests S4 class definitions, setGeneric and setMethod dispatch, reduction to lowest terms, and integer edge cases such as zero and overflow.

Implement a normalized S4 Rational number class in R

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

In `/app/solution.R` (load `library(methods)`), implement an S4 class `Rational` with a constructor and arithmetic generics. Every stored value is normalized: reduced to lowest terms (divide by the gcd) with a positive denominator (the sign goes on the numerator). The file must define: - Class `Rational` with integer slots `num` and `den`. Its validity method rejects `den == 0` and requires both slots to have length 1. - `make_rational(num, den)`: coerce both arguments to integer, normalize (`make_rational(2, -4)` stores `num = -1L`, `den = 2L`), and error via validity when `den == 0`. - `rat_numerator(x)` and `rat_denominator(x)`: S4 generics with methods returning the integer slot values. - `rat_add(a, b)` and `rat_multiply(a, b)`: S4 generics with methods returning a reduced `Rational`. - `rat_to_double(x)`: S4 generic with a method returning the numeric scalar `num/den`. It is the only function that returns `numeric`. Example: ```r a <- make_rational(1, 2) b <- make_rational(2, -4) # normalized to -1/2 rat_numerator(b) # -1L rat_denominator(b) # 2L rat_to_double(rat_add(a, b)) # 0 rat_to_double(rat_multiply(a, a)) # 0.25 (1/4) ``` ```hint Where to normalize Count the places in your file that can create a `Rational`. If every one of them goes through the same function, the canonical form only has to be enforced once. ``` ```hint Zero and sign Trace what your gcd returns when the numerator is 0, and decide what happens to the sign before the gcd runs when the denominator is negative. ``` ### Constraints & Assumptions - Slots are R integers, so every stored numerator and denominator must lie within ±2,147,483,647 (`.Machine$integer.max`). - The code lives in `/app/solution.R` and calls `library(methods)` before defining the class. ### What a Strong Answer Covers - A class definition with typed integer slots and a validity method that checks slot lengths and a zero denominator - One normalization path (sign to the numerator, divide by the gcd) that the constructor and every arithmetic result go through - Correct `setGeneric` and `setMethod` calls, including two-argument signatures for `rat_add` and `rat_multiply` - Correct results for zero numerators and negative denominators, with integer (not double) values in the slots - Awareness that intermediate products can overflow 32-bit integers ### Follow-up Questions - How would you overload `+` and `show()` for `Rational`? - Why prefer S4 over S3 for this class? - `rat_add` multiplies the two denominators together. How would you keep intermediate values from overflowing? - How would you add an equality comparison, and why does normalization make it simple?

Overview: Implement an S4 Rational class in R with a validity method, a normalizing constructor, and generics for the accessors, addition, multiplication and conversion to double. It tests S4 class definitions, setGeneric and setMethod dispatch, reduction to lowest terms, and integer edge cases such as zero and overflow.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

In /app/solution.R (load library(methods)), implement an S4 class Rational with a constructor and arithmetic generics. Every stored value is normalized: reduced to lowest terms (divide by the gcd) with a positive denominator (the sign goes on the numerator).

The file must define:

  • Class Rational with integer slots num and den . Its validity method rejects den == 0 and requires both slots to have length 1.
  • make_rational(num, den) : coerce both arguments to integer, normalize ( make_rational(2, -4) stores num = -1L , den = 2L ), and error via validity when den == 0 .
  • rat_numerator(x) and rat_denominator(x) : S4 generics with methods returning the integer slot values.
  • rat_add(a, b) and rat_multiply(a, b) : S4 generics with methods returning a reduced Rational .
  • rat_to_double(x) : S4 generic with a method returning the numeric scalar num/den . It is the only function that returns numeric .

Example:

a <- make_rational(1, 2)
b <- make_rational(2, -4)   # normalized to -1/2
rat_numerator(b)            # -1L
rat_denominator(b)          #  2L
rat_to_double(rat_add(a, b))       # 0
rat_to_double(rat_multiply(a, a))  # 0.25  (1/4)

Constraints & Assumptions

  • Slots are R integers, so every stored numerator and denominator must lie within ±2,147,483,647 ( .Machine$integer.max ).
  • The code lives in /app/solution.R and calls library(methods) before defining the class.

What a Strong Answer Covers Guidance

  • A class definition with typed integer slots and a validity method that checks slot lengths and a zero denominator
  • One normalization path (sign to the numerator, divide by the gcd) that the constructor and every arithmetic result go through
  • Correct setGeneric and setMethod calls, including two-argument signatures for rat_add and rat_multiply
  • Correct results for zero numerators and negative denominators, with integer (not double) values in the slots
  • Awareness that intermediate products can overflow 32-bit integers

Follow-up Questions Guidance

  • How would you overload + and show() for Rational ?
  • Why prefer S4 over S3 for this class?
  • rat_add multiplies the two denominators together. How would you keep intermediate values from overflowing?
  • How would you add an equality comparison, and why does normalization make it simple?