PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

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.

  • easy
  • Pinterest
  • Coding & Algorithms
  • Machine Learning Engineer

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.

Input: ("2.5",)

Expected Output: "3"

Explanation: Tie: round half away from zero gives 3.

Input: ("-2.5",)

Expected Output: "-3"

Explanation: Tie on a negative magnitude rounds away from zero to -3.

Input: ("0.4",)

Expected Output: "0"

Explanation: First fractional digit 4 < 5, so truncate to 0.

Input: ("-0.6",)

Expected Output: "-1"

Explanation: First fractional digit 6 >= 5, magnitude rounds up to 1, sign preserved.

Input: ("99.9",)

Expected Output: "100"

Explanation: Carry propagates across all 9s, producing a new leading digit.

Input: ("+123.456",)

Expected Output: "123"

Explanation: Explicit '+' sign is dropped; first fractional digit 4 < 5 truncates.

Input: ("123456789012345678901234567890.7",)

Expected Output: "123456789012345678901234567891"

Explanation: 30-digit integer part that overflows float(); string carry adds exactly 1.

Input: ("5",)

Expected Output: "5"

Explanation: No decimal point at all; the value is returned unchanged.

Input: ("-0.4",)

Expected Output: "0"

Explanation: -0.4 truncates to magnitude 0, and -0 is normalized to 0.

Input: (".5",)

Expected Output: "1"

Explanation: Empty integer part treated as 0; tie 0.5 rounds away from zero to 1.

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

Input: ("12549", "100")

Expected Output: "12500"

Explanation: exp=2; first dropped digit '4' < 5, so truncate to 12500.

Input: ("12550", "100")

Expected Output: "12600"

Explanation: exp=2; first dropped digit '5' rounds the prefix up to 126, giving 12600.

Input: ("999", "100")

Expected Output: "1000"

Explanation: Carry across all 9s creates a new leading digit before appending the trailing zeros.

Input: ("1234.678", "0.01")

Expected Output: "1234.68"

Explanation: k=2 decimals; dropped digit '8' (>=5) rounds 67 up to 68.

Input: ("-1234.678", "0.1")

Expected Output: "-1234.7"

Explanation: Negative magnitude rounds away from zero; sign preserved on a non-zero result.

Input: ("5", "1000")

Expected Output: "0"

Explanation: Edge case: value far smaller than p; the rounding digit is a virtual leading zero so it rounds down to 0.

Input: ("500", "1000")

Expected Output: "1000"

Explanation: exp=3; the top digit '5' at the rounding place rounds the magnitude up to one multiple of 1000.

Input: ("0.04", "0.1")

Expected Output: "0.0"

Explanation: k=1; first dropped digit '4' < 5 truncates, but one decimal place is preserved as '0.0'.

Input: ("0.05", "0.1")

Expected Output: "0.1"

Explanation: k=1; first dropped digit '5' rounds the tenths place up to 1.

Input: ("7", "1")

Expected Output: "7"

Explanation: exp=0; rounding to the nearest 1 leaves an integer unchanged.

Input: ("-0.04", "0.1")

Expected Output: "0.0"

Explanation: Rounds to magnitude zero; -0.0 is normalized to a positive '0.0'.

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'.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • First Word Matching Each Prefix Query - Pinterest (medium)
  • Hierarchical Access Control for an Advertising Platform - Pinterest (medium)
  • Maximize Boxes Stored Through One Entrance - Pinterest (medium)
  • Solve Multiple Coding Interview Problems - Pinterest (medium)
  • Implement a Sparse Matrix Class - Pinterest (medium)