</> 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.

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.