Implement string-based rounding without floats
Company: Pinterest
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string-based numeric processing, arbitrary-precision arithmetic concepts, precise rounding rules including deterministic tie-breaking, and handling of sign and decimal edge cases, and falls under the Coding & Algorithms domain.
String-Based round() to Nearest Integer
Constraints
- Do not parse the input with float()/double/Number; operate on the string directly.
- Input matches an optional sign, then digits and/or a single '.', e.g. '+', '-', '.', and 0-9.
- The integer part may be arbitrarily long (longer than any native numeric type).
- Tie-breaking is round half away from zero (first fractional digit >= 5 rounds the magnitude up).
Examples
Input: ("-.2",)
Expected Output: "0"
Explanation: -0.2 rounds toward 0; the sign is dropped because the magnitude is zero.
Input: ("2.",)
Expected Output: "2"
Explanation: Trailing decimal point with no fractional digits; value is exactly 2.
Hints
- Strip the sign first, then split on the decimal point into an integer part and a fractional part; either side may be empty ('-.2' has empty integer part, '2.' has empty fractional part).
- Only the FIRST fractional digit matters for rounding to the nearest integer: if it is 5-9, increment the integer-part string by one with manual carry propagation.
- Remember to re-normalize at the end: drop leading zeros and turn a magnitude of '0' back into a signless '0' so you never return '-0'.
String-Based Round to Nearest Multiple of a Power of Ten
Constraints
- Do not parse s or p with float()/double/Number; operate on the strings directly.
- p is guaranteed to be a positive power of ten: '1', '10', '100', '1000', ... or '0.1', '0.01', ...
- s may carry a sign, an arbitrarily long integer part, and an optional fractional part.
- Rounding is half away from zero; output keeps exactly k decimals when p = 10^-k, else is a plain integer.
Examples
Input: ("12567", "100")
Expected Output: "12600"
Explanation: exp=2; digit at place 1 is '6' (>=5) so the kept prefix '125' becomes '126', then two trailing zeros.
Input: ("1234.678", "0.1")
Expected Output: "1234.7"
Explanation: k=1 decimal; kept fractional digit '6', next dropped digit '7' (>=5) rounds it to '7'.
Hints
- First convert p into an integer exponent: '100' -> exp 2, '1' -> exp 0, '0.1' -> exp -1, '0.01' -> exp -2. That exponent is the place you round to.
- Split into the exp >= 0 case (round inside/beyond the integer part, output an integer with exp trailing zeros) and the exp < 0 case (keep k = -exp decimal digits). The single 'first dropped digit' at the rounding place decides whether to add one.
- Watch the case where the value is far smaller than p (e.g. round('5','1000')): the first dropped digit is a virtual leading zero, so it must round DOWN to '0', not up to '1000'.