</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Digit and Number Math

What is this?

These tricky problems look like they need a loop or simulation, but each one hides a number identity that makes the loop disappear. The single insight is to stop simulating and count the right thing โ€” a digital root that's just a value mod 9, a count of one prime factor, or a single greedy digit swap. Recognizing the closed-form shortcut is the whole game.

flowchart TD A["A number problem that looks iterative"] --> B["Spot the identity such as mod 9 digital root"] A --> C["Count the scarce prime factor such as 5s"] A --> D["Make one greedy digit choice"] B --> E["Formula or single pass replaces the loop"] C --> E D --> E

๐Ÿ’ก Fun fact: Repeatedly summing a number's digits until one remains always equals the number modulo 9 โ€” because every power of ten leaves a remainder of 1 when divided by 9, so the digits and the whole number are congruent mod 9.

๐Ÿ”“ The 3 problems in this chapter are free. Sign in with Google or Microsoft to start solving.


Core idea: Many "number" problems hide a closed-form identity. Instead of simulating the process, you spot the underlying arithmetic โ€” a mod-9 root, a count of a single prime factor, a greedy digit choice โ€” and the loop collapses to a formula or a single pass.


The pattern

The reframe here is stop simulating, start counting the right thing. Repeatedly summing digits until one remains is just the value modulo 9 (the digital root), because each power of ten is congruent to 1 mod 9. The whole iterative process becomes one expression.

The same instinct applies to factorials: trailing zeros come from factors of ten, and tens are scarcer in their 5 than their 2, so you only count how many 5s the factorial contributes โ€” including the extra 5s from 25, 125, and so on. You never build the huge number.

For rearranging digits, the move is greedy with the right target: scan for the leftmost digit that can be improved, and swap it with the largest later digit (preferring the rightmost occurrence of that largest digit to maximize the gain). One swap, chosen carefully, beats searching all pairs. The thread throughout: find the identity or the single decisive move.


The problems

  • Add Digits โ€” the repeated digit sum is the digital root, computable as 1 + (n-1) % 9 for positive n; no looping.
  • Factorial Trailing Zeroes โ€” count factors of 5 across the factorial (n/5 + n/25 + n/125 + ...), since 2s always outnumber 5s.
  • Maximum Swap โ€” greedily find the leftmost digit smaller than some later digit, then swap it with the rightmost occurrence of the largest such digit.

Key takeaways

  • Look for the identity โ€” digital root is mod 9, not a loop.
  • Count the scarce factor โ€” trailing zeros track 5s, not 10s.
  • Greedy needs the right target โ€” leftmost improvable, rightmost-largest.
  • Don't build the big number โ€” reason about its factors instead.

Core idea: Repeatedly summing a number's digits collapses it to its digital root, and because a number is congruent to its digit sum modulo 9, that root is just 1 + (n - 1) % 9 (with 0 for 0) โ€” no loop required.

Problem, rephrased

You're closing the books at a warehouse. Every shipment ID is a long number, and your old-school inventory clerk verifies entries by adding up the digits over and over until a single digit is left โ€” the classic "casting out nines" check used on paper ledgers. Given a shipment ID like 38, she computes 3 + 8 = 11, then 1 + 1 = 2, and writes down 2.

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. Return that digit (its digital root).

Input Process Output
0 already one digit 0
5 already one digit 5
38 3+8=11 โ†’ 1+1=2 2
99 9+9=18 โ†’ 1+8=9 9
12345 1+2+3+4+5=15 โ†’ 1+5=6 6

The statement is simple. The follow-up is the whole point: could you do it without any loop or recursion, in O(1)?

Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.