DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
MediumJavaScript

Your Scientific Calculator Handles Signed Arbitrary-Precision Addition Including Negatives

2 views

Technical Interview Challenge: The "Signed Ledger" Arithmetic Engine

Context

In advanced financial engineering and scientific computing, precision is non-negotiable. Standard JavaScript numbers use the IEEE 754 format, which loses integer precision beyond $2^{53} - 1$. While unsigned "Big Addition" is a common problem, real-world systems must handle signed arithmetic—processing both credits (positive) and debits (negative) of arbitrary magnitude.

You are building the core arithmetic module for a high-precision calculator. This module must handle the mathematical signs manually, effectively simulating how a CPU performs arithmetic at the bit level, but using decimal strings.


Problem Statement

Implement a function bigAddSigned(a, b) that takes two strings representing signed integers and returns their sum as a string.

Requirements:

  1. Sign Handling: Correctly process inputs that are positive (e.g., "123"), negative (e.g., "-456"), or zero ("0").

  2. No Native BigInt: You are strictly forbidden from using the BigInt constructor or BigInt literals.

  3. Arbitrary Precision: The strings may contain hundreds of digits, far exceeding the limits of the Number type.

  4. Mathematical Correctness:

    • Adding two negatives: (-A) + (-B) = -(A + B)

    • Adding a positive and a negative: A + (-B) = A - B

  5. Clean Output: Resulting strings should not have unnecessary leading zeros (e.g., return "0", not "-0" or "007").


Example Use Cases

Example 1: Opposite Signs (Subtraction Logic)

JavaScript

bigAddSigned("123", "-456"); 
// Expected Output: "-333"

Example 2: Both Negative (Addition Logic)

JavaScript

bigAddSigned("-100", "-200"); 
// Expected Output: "-300"

Example 3: Zero Result

JavaScript

bigAddSigned("999", "-999"); 
// Expected Output: "0"

Example 4: Large Magnitude

JavaScript

bigAddSigned("-5000000000000000000", "10000000000000000000"); 
// Expected Output: "5000000000000000000"

Interview Evaluation Criteria

  • Algorithm Selection: How do you decide between using a "Big Addition" helper versus a "Big Subtraction" helper?

  • Magnitude Comparison: To compute $A - B$ where $B > A$, you must recognize that the result will be $-(B - A)$. How do you efficiently compare the magnitude of two numeric strings?

  • Edge Case Management: How do you handle cases where the result is 0 or when one input has a sign and the other does not?

  • Modular Code: Do you break the problem into smaller, reusable pieces (e.g., compare(a, b), add(a, b), and subtract(a, b))?

Sample Test Cases

Case 1
Input
a="123", b="-456"
Expected Output
"-333"
Case 2
Input
a="-100", b="-200"
Expected Output
"-300"
Case 3
Input
a="999", b="-999"
Expected Output
"0"

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
2
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering

Languages

JavaScript