</> 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: Every trailing zero in n! is one factor of 10, and 10 = 2 ร— 5. Factors of 2 are plentiful and factors of 5 are scarce, so the number of trailing zeros is exactly the number of 5s โ€” and you count those with n//5 + n//25 + n//125 + โ€ฆ, never building the factorial at all.

A "trailing zero" is a zero at the end of a number: 120 has one, 1000 has three. The number n! (n factorial = 1 ร— 2 ร— 3 ร— โ€ฆ ร— n) grows astronomically fast โ€” 25! already has 26 digits โ€” so the trap in this problem is the obvious idea: compute n!, then count the zeros on the end. That overflows fixed-width integers almost immediately and is needlessly slow even with big integers. The whole lesson is about not building the factorial, and instead reasoning about where the zeros come from.


Problem, rephrased

You're building an analytics tool that reports factorials of large counts (think permutation totals in a scheduling engine). The full number is too big to show, but a product manager wants a quick "how many zeros does it end in?" stat next to each value. Given a single integer n, return the number of trailing zeros in n! โ€” and do it in logarithmic time, without ever materializing the factorial.

Input n n! Output Why
3 6 0 3! = 6, no zero on the end
5 120 1 one trailing zero
10 3628800 2 two trailing zeros
25 (26 digits) 6 25 alone contributes two 5s
0 1 0 0! = 1 by convention

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.