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,BigDecimalor 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
- Decide how the digits are added and how the result is built before any code is written, and say it in your request.
- State the zero rule and the final carry yourself, with the contract's edge cases and their expected outputs.
- Ask for failing tests of those cases first, and press Test to watch the run fail.
- Name the failing case and its cause, then spell out the fix.
- Press Test again after the last change, then Submit.
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
Where a first version goes wrong
- Parsing an operand into a long overflows past 18 digits, and handing the sum to
BigIntegeris 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
- 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.
- 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.
- Put the carry left at the end into the first slot, which holds a zero otherwise.
- 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
- 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
- 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
- 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.