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

Quick Answer: 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...