Quick Overview

This question evaluates parsing and numeric manipulation skills, specifically implementing rounding rules for numeric strings and handling decimals, significant digits, and precision edge cases.

Round numeric string values

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

##### Question Given a numeric string, implement rounding to the nearest integer (e.g., '3.5' → '4', '100.01' → '100'). Follow-up: round to the last significant digit (e.g., 124900 → 125000, 12.49 → 12. 5).

Overview: This question evaluates parsing and numeric manipulation skills, specifically implementing rounding rules for numeric strings and handling decimals, significant digits, and precision edge cases.

Implement a function round_numeric_string(s, mode) that rounds a base-10 numeric string s according to mode. If mode == "integer", round to the nearest integer with ties at exactly 0.5 rounded away from zero (e.g., "3.5" -> "4", "-2.5" -> "-3"). If mode == "last_sig_digit", reduce precision by one significant digit using half-up rounding: identify the first and last non-zero digits of s (ignoring leading zeros and the decimal point). If there are at least two significant digits, drop the last significant digit by rounding the preceding digit, then zero-out less significant places (e.g., "124900" -> "125000", "12.49" -> "12.5"). If there is only one significant digit, return the canonical form of s. The result must be minimally formatted: no leading zeros (except a single zero before the decimal point), no trailing zeros after the decimal point, and no trailing decimal point. Return "0" instead of "-0". The input s may include an optional leading sign and at most one decimal point; no exponent notation.

Constraints

  • 1 <= len(s) <= 100000
  • s matches the regex ^[+-]?\d+(\.\d+)?$ (no exponent, no separators)
  • mode is either "integer" or "last_sig_digit"
  • Rounding rule for mode="integer": nearest integer, ties at .5 round away from zero
  • Rounding rule for mode="last_sig_digit": half-up on the last significant digit; if only one significant digit exists, return canonical s
  • Output formatting: remove leading zeros (keep one before '.'), remove trailing zeros after '.', remove trailing '.', never return "-0"

Examples

Input:

Expected Output: 4

Input:

Expected Output: 100

Hints

  1. Avoid floating-point; work directly on the string digits.
  2. For integer rounding, comparing only the first digit after the decimal to 5 suffices to decide <0.5 vs >=0.5.
  3. Represent the number as a digit array plus the decimal index. For last_sig_digit, find the first and last non-zero positions.
  4. When reducing precision by one significant digit, round the digit before the last non-zero and propagate carry left; then zero-out all less significant digits.
  5. Normalize the final string: strip leading zeros in the integer part (keep one zero), strip trailing zeros in the fractional part, and omit the decimal point if the fractional part becomes empty.

Loading coding console...

Show the approach

Approach

We avoid floating-point arithmetic by operating on the string's digits directly. We parse the sign, collect all digits into an array, and remember the decimal point index. For mode="integer", we inspect only the first digit after the decimal to decide whether the fractional part is < 0.5 or >= 0.5; on >= 0.5 we increment the integer digits (away from zero for negatives as well). For mode="last_sig_digit", we find the first and last non-zero digits to determine the current significant-digit span. If there is just one significant digit, we return the canonical form. Otherwise, we round the digit just before the last non-zero and propagate carry left as needed, then zero out less significant places (which may introduce carry to a new most significant digit). Finally, we format by stripping leading zeros in the integer part (keeping one zero), removing trailing zeros from the fractional part, and omitting the decimal point if there is no fractional part. We also normalize -0 to 0.

Time complexity:
O(n)
Space complexity:
O(n)