Skip to content
Recommended solution · HC-1 · Java

Just Two Sum

A small implementation task in Java: add two decimal numbers of up to a million digits each, given as one string joined by a plus sign, without a big-number library. The arithmetic is easy; the marks are in deciding how to add and how to build the result, stating the zero and carry rules, and checking the edge cases yourself.

On this page

The codebase

The workspace holds one class with one method to implement, and an empty test class of your own. The supplied tests are not in the workspace: Test compiles and runs them, shows each failing case with its input, expected and actual value, and stops at the first failure.

Test runs your own tests after all 45 supplied ones pass. Verification after Submit adds 19 hidden tests, among them a million-digit case with a carry in every position, then runs your tests too.

The assistant explains the code and Java on request and makes the changes you direct. It writes tests only in your test class, and asks which cases when you name none.

Where things live

src/JustTwoSum.javaThe class to implement: add takes the expression and returns the exact sum as decimal digits.
candidate_tests/JustTwoSumCandidateTest.javaYour own JUnit 5 tests, each with a limit of 10 seconds. Empty until you add some.

Rules that shape every fix

  • Implement the addition yourself: no BigInteger, BigDecimal or other arbitrary-precision library.
  • Aim for time proportional to the input: each operand has up to a million digits, and the result up to a million and one.
  • Every input follows the contract, so there is nothing to validate: two runs of digits joined by one plus sign, leading zeros allowed.

How to work the task

  1. Decide how the digits are added and how the result is built before any code is written, and say it in your request.
  2. State the zero rule and the final carry yourself, with the contract's edge cases and their expected outputs.
  3. Ask for failing tests of those cases first, and press Test to watch the run fail.
  4. Name the failing case and its cause, then spell out the fix.
  5. Press Test again after the last change, then Submit.
Stage 1 · just-two-sum

Add two million-digit numbers

What the ticket asks

Implement JustTwoSum.add in src/JustTwoSum.java: the expression is two non-negative decimal integers joined by a single plus sign, such as 1234567890+0987654321, and the result is their exact sum as decimal digits.

Operands have up to one million digits each, and the arithmetic must be your own: no arbitrary-precision library.

Where to look

src/JustTwoSum.javaThe stub throws. Everything you write goes here, and nothing else in the file is needed.
candidate_tests/JustTwoSumCandidateTest.javaWhere your own cases become runnable tests. Test runs them once the supplied tests pass.

What the contract pins down

The sumThe exact sum as decimal digits, of operands with 1 to 1,000,000 digits each: 1234567890+0987654321 is 2222222211.
ZerosLeading zeros are allowed in the operands, and the result has none: 00012+00008 is 20, and a zero sum is a single 0, so 000+00 is 0.
Carry and lengthsA carry past the last digit adds a digit, so 999+1 is 1000, and the operands may differ in length either way: 7+12345 and 12345+7 are both 12352.
TimeEvery test has 10 seconds and the supplied ones include million-digit operands, so the work has to stay linear in the input.

Where a first version goes wrong

  • Parsing an operand into a long overflows past 18 digits, and handing the sum to BigInteger is outside the task.
  • Prepending each digit to a string, or inserting at the front of a builder, copies the whole result every step: quadratic, and the million-digit tests time out.
  • A loop that stops when the shorter operand ends, or forgets the carry left after the last column, loses digits: 999+1 comes back as 000 or 1000 without its first digit.
  • Trimming leading zeros without keeping the last digit turns 000+00 into an empty string.

The fix

  1. Find the plus sign once and read the digits in place from the end of each operand, with no split, substring or copy of an operand.
  2. Fill one character array, one slot longer than the longer operand, from its last slot backwards, adding the two digits and the carry at each step.
  3. Put the carry left at the end into the first slot, which holds a zero otherwise.
  4. Skip the result's leading zeros but never its last digit, and build the string from the array once.
src/JustTwoSum.javaThe change the right path shipped: one pass from the right over both operands into a preallocated buffer, the final carry in its first slot, then one string from the first significant digit.
     public static String add(String expression) {-        throw new UnsupportedOperationException("Implement addition");+        int plus = expression.indexOf('+');+        int leftLen = plus;+        int rightLen = expression.length() - plus - 1;+        char[] out = new char[Math.max(leftLen, rightLen) + 1];+        int i = plus - 1;+        int j = expression.length() - 1;+        int k = out.length - 1;+        int carry = 0;+        while (i >= 0 || j > plus) {+            int sum = carry;+            if (i >= 0) {+                sum += expression.charAt(i--) - '0';+            }+            if (j > plus) {+                sum += expression.charAt(j--) - '0';+            }+            out[k--] = (char) ('0' + sum % 10);+            carry = sum / 10;+        }+        out[0] = (char) ('0' + carry);+        int start = 0;+        while (start < out.length - 1 && out[start] == '0') {+            start++;+        }+        return new String(out, start, out.length - start);     }

Tests that prove it

  • The final carry: 999+1 is 1000.
  • The canonical zero and leading zeros: 000+00 is 0, and 00012+00008 is 20.
  • Unequal lengths both ways: 7+12345 and 12345+7 are both 12352.
  • The first example, 1234567890+0987654321, is 2222222211.

The conversation

Only your side is shown: the assistant reads, writes and runs between your messages. Each chip is a rubric item the step earns.

DirectionPrompt qualityVerificationCorrectnessCode quality

  1. Your message 1

    Before any code, here is my plan for add; tell me if anything in it breaks the contract, without editing yet. Add digit by digit from the right with a carry, never parsing an operand into a number or a BigInteger. Find the single '+' with indexOf and read the digits in place with charAt, walking i from the end of the left operand and j from the end of the right one, so there is no split, substring or operand copy. Build the result in place: one char[] of length max(lenA, lenB) + 1 filled from its last slot backwards, never prepending to a String. Zero and carry rules: when both operands are used up, a carry that is still 1 goes into slot 0 (the final carry), and then I skip the result's leading zeros, keeping the last digit, so a zero sum is "0". The cases I care about: 999+1 → 1000 (final carry), 000+00 → 0 (canonical zero), 00012+00008 → 20 (leading zeros), 7+12345 → 12352 and 12345+7 → 12352 (unequal lengths both ways).

    Your plan, your zero and carry rules, and every edge-case family with its expected output, before any code. The assistant's review found one gap: the first slot needs a zero when there is no final carry.

    • Direction: Digit addition, 6 points
    • Direction: Linear result, 6 points
    • Direction: Zeros and carry, 6 points
    • Direction: Ownership, 6 points
    • Prompt quality: Specified add, 6 points
    • Prompt quality: Staged or complete, 4 points
    • Prompt quality: Contract examples, 5 points
    • Verification: Named cases, 8 points
  2. Your message 2

    Good, the plan stands. Before src/JustTwoSum.java changes, write failing tests in candidate_tests/JustTwoSumCandidateTest.java, one @Test per case, each calling JustTwoSum.add and assertEquals on the exact string: 999+1 → "1000", 000+00 → "0", 00012+00008 → "20", 7+12345 → "12352", 12345+7 → "12352", and 1234567890+0987654321 → "2222222211". Don't change src/JustTwoSum.java yet.

    Your cases become runnable tests before the code exists; they count once Test runs them.

  3. Press Test. The stub throws, so the first supplied case fails and the run stops there: your own tests have not run yet.
    • Verification: Reaction to failure, 4 points
  4. Your message 3

    Test fails at 1951+49 = 2000: for "1951+49" it expected "2000" and got . That fails because add still throws: nothing is implemented yet, so every supplied case fails the same way and my own tests have not run. Now implement the plan in src/JustTwoSum.java: int plus = expression.indexOf('+'); i = plus - 1 down to 0 and j = expression.length() - 1 down to plus + 1; char[] out of length max(plus, expression.length() - plus - 1) + 1; at each slot k from the end, sum = carry + (i >= 0 ? expression.charAt(i--) - '0' : 0) + (j > plus ? expression.charAt(j--) - '0' : 0), out[k] = (char) ('0' + sum % 10), carry = sum / 10, until both operands are used up; put the final carry in out[0] (it is '0' otherwise); then skip leading '0' chars but stop at the last slot, and return new String(out, start, out.length - start). No BigInteger, no parse of an operand, no substring or split, no prepending, nothing else in the file.

    Name the failing case and why it fails, then spell out the fix, the first slot's zero included.

    • Prompt quality: Failure follow-up, 5 points
    • Code quality: Code quality, 10 points
  5. Press Test. All 51 tests pass after the last change: the 45 supplied ones, then your six.
    • Verification: Run after last change, 6 points
    • Verification: Checks own, 2 points
  6. Submit. The stage closes. Scope and the hidden tests are judged on the final code: 70 of 70 passed, the 19 hidden ones and your six included.
    • Direction: Scope, 6 points
    • Correctness: Hidden tests, 20 points

Where the stage lands

Measured when this conversation was played on the platform. A+ needs 92, S needs 95.

Direction30/30
Prompt quality20/20
Verification20/20
Correctness20/20
Code quality10/10
Stage total 100/100Grade S
Just Two Sum Solution — Java Walkthrough | Hunicode