Reported September 2022
ZipRecruitermath

Sum All Ordered Decimal Concatenations

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

The ZipRecruiter OA reported in September 2022 looks like a brute-force pair loop, and with 100000 values that loop dies. The real solution hinges on a counting structure: a hash map or array that tracks how many numbers have each digit length. Concatenating a and b means a * 10^len(b) + b. Once you see that, the n squared pairs collapse into arithmetic. If you blank on the setup, StealthCoder runs invisibly during the live assessment and gives you the approach as a safety net. Know the trick and the code is about fifteen lines.

The problem

Given positive integers values, consider every ordered pair of elements, including pairing an index with itself. Concatenate their decimal representations in pair order and return the sum of all resulting numbers.

Function
sumOrderedConcatenations(values: int[]) → long

Examples
Example 1
values = [2,10,3]
return = 1845
The nine ordered concatenations sum to 1845.
Example 2
values = [7]
return = 77
The only ordered pair concatenates 7 with itself.

Constraints
1 <= values.length <= 100000
1 <= values[i] <= 1000000000
The answer fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Split each concatenation into two parts. For ordered pair (a, b), the value is a * 10^len(b) + b. Summing over all pairs, the second part contributes n * sum(values), since each b appears with every a. The first part needs, for each digit length L from 1 to 10, the count of numbers with that length. Then it contributes sum(values) * sum over L of count[L] * 10^L. So you only need the total sum and a length-count table of size 10. That's O(n). The common pitfall is overflow in intermediate steps, but the statement says the answer fits in signed 64-bit, so long is fine if you sum in the right order. Another trap is forgetting the self-pair, which the formula already includes. Check against [7]: 7 * 10 + 7 = 77. If the formula slips under pressure, StealthCoder is the hedge during the live OA.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Sum All Ordered Decimal Concatenations cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

ZipRecruiter reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Sum All Ordered Decimal Concatenations FAQ

How hard is the ZipRecruiter Sum All Ordered Decimal Concatenations problem really?+

Easy to medium once you see the decomposition. The hard part is resisting the n squared loop. With n up to 100000, brute force is about ten billion pairs. The math version is a single pass plus a tiny table, so the code is short.

What's the trick to solving it fast?+

Write concat(a, b) as a * 10^len(b) + b. Then sum the two terms separately. The b term gives n times the total sum. The a term uses the total sum times the sum of 10^len(b) over all b. Count digit lengths once and you're done.

Do I need a hash map or is an array enough?+

An array of size 11 is enough. Values go up to 1000000000, which has 10 digits, so lengths run from 1 to 10. Index by digit length and store counts. A hash map works too but adds nothing here.

Where do people get wrong answers on this one?+

Usually they skip the pair where an index pairs with itself, or they get the power of ten wrong by using len(a) instead of len(b). Test with [7], which must return 77, and with [2,10,3], which must return 1845.

How do I prepare for this in 48 hours?+

Practice the pattern of splitting a pair sum into per-element contributions and counting by a small key like digit length. Do two or three problems of that style, then hand-verify this one on both examples. It's a pattern, not a memorization job.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it