Goldman Sachs Online Assessment Question | String | GCD | Maths

goldman sachs logo
goldman sachs
Ongoing
August 26, 202518 reads

Summary

I recently encountered a problem during my Goldman Sachs Online Assessment related to simplifying fractions, which required applying GCD concepts.

Full Experience

During my recent online assessment for Goldman Sachs, I was presented with a problem that involved simplifying fractions. The core task was to take a string in the format "a/b" and reduce it to its lowest terms, ensuring the denominator remains positive. It was a straightforward implementation once the logic for handling negative numbers and using the greatest common divisor (GCD) was clear.

Interview Questions (1)

Q1
Simplify Fraction
Data Structures & AlgorithmsEasy

Problem: Simplify Fraction

You are given a string s in the form "a/b", where a and b are integers (which may be positive or negative).

Your task is to reduce the fraction into its lowest terms.

  • If the fraction reduces to an integer, return the integer (as a string).
  • If not, return the simplified fraction in the form "p/q".
  • Ensure the denominator is always positive (move any negative sign to the numerator).

Examples

Input: "13/12"
Output: "13/12"

Input: "4/6"
Output: "2/3"

Input: "10/100"
Output: "1/10"

Input: "100/400"
Output: "1/4"

Input: "7/35"
Output: "1/5"

Input: "-12/13"
Output: "-12/13"

Input: "12/-4"
Output: "-3"

Input: "-8/-4"
Output: "2"

Discussion (0)

Share your thoughts and ask questions

Join the Discussion

Sign in with Google to share your thoughts and ask questions

No comments yet

Be the first to share your thoughts and start the discussion!