Quick Overview

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.

Implement string-based rounding without floats

Company: Pinterest

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

### Coding You are not allowed to parse the input into a built-in floating type (to avoid overflow and precision issues). Work directly on strings. #### 1) Implement `round(s)` from scratch Given a string `s` representing a decimal number, return a string representing the value **rounded to the nearest integer**. - `s` may include an optional sign (`+`/`-`) and an optional decimal point. - Examples of tricky inputs you must handle: - `"-.2"` (equivalent to `-0.2`) - `"2."` (equivalent to `2.0`) - Very long integer parts that would overflow `float()` Clarify and implement a deterministic tie-breaking rule (e.g., exactly `.5` cases). #### 2) Round a numeric string `s` to a given precision `p` Given two strings: - `s`: a decimal number as a string - `p`: a positive decimal precision as a string Return `s` rounded to the nearest multiple of `p` (as a string). Examples: - `s = "12567"`, `p = "100"` → return `"12600"` - `s = "1234.678"`, `p = "0.1"` → return `"1234.7"` Assume `p` is a power of 10 (e.g., `1000`, `0.01`, `0.1`). Specify how you format the output (e.g., whether to keep trailing zeros).

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

You are not allowed to parse the input into a built-in floating-point type (to avoid overflow and precision issues). Work directly on the string. Given a string `s` representing a decimal number, return a string representing the value **rounded to the nearest integer**. Rules: - `s` may include an optional leading sign (`+` or `-`) and an optional decimal point. - Tie-breaking is **round half away from zero**: the first fractional digit decides — if it is `5`-`9` round the magnitude up, otherwise truncate. - The result must not parse `s` with `float()`; the integer part may be arbitrarily long. - Normalize the output: no leading `+`, strip leading zeros, and represent zero as `"0"` (never `"-0"`). Examples: - `"-.2"` -> `"0"` - `"2."` -> `"2"` - `"2.5"` -> `"3"`, `"-2.5"` -> `"-3"` - `"123456789012345678901234567890.7"` -> `"123456789012345678901234567891"`

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

  1. 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).
  2. 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.
  3. 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

Continuing the no-float rule: work directly on strings. Given two strings: - `s`: a decimal number - `p`: a positive precision that is a power of ten (e.g. `"1000"`, `"100"`, `"1"`, `"0.1"`, `"0.01"`) Return `s` rounded to the nearest multiple of `p`, as a string. Output formatting: - If `p >= 1` the result is an integer string (with the appropriate trailing zeros), e.g. `round("12567", "100") -> "12600"`. - If `p < 1` the result keeps **exactly k decimal places**, where `p = 10^-k`, preserving trailing zeros, e.g. `round("1234.678", "0.1") -> "1234.7"` and `round("0.04", "0.1") -> "0.0"`. - Rounding is half away from zero, decided by the first dropped digit. - Never return `"-0"` / `"-0.0"`; normalize a zero magnitude to a positive sign. Examples: - `s="12567", p="100"` -> `"12600"` - `s="1234.678", p="0.1"` -> `"1234.7"` - `s="5", p="1000"` -> `"0"`

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

  1. 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.
  2. 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.
  3. 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'.

Loading coding console...