The Count Good Numbers interview question defines a "good" number as one where digits at even indices (0, 2, 4...) are even (0, 2, 4, 6, 8) and digits at odd indices (1, 3, 5...) are prime (2, 3, 5, 7). Given a length n, you need to find the total number of good numbers of that length. Return the answer modulo 10^9 + 7.
Amazon and Google ask the Recursion / Math interview pattern for this problem to test your ability to handle large exponents and modular arithmetic. Since n can be up to 10^15, you must use Binary Exponentiation. It evaluates whether you can derive a simple combinatorial formula and implement it efficiently.
This is a Combinatorics and Binary Exponentiation problem.
ceil(n / 2).floor(n / 2).0, 2, 4, 6, 8).2, 3, 5, 7).(5^even_pos * 4^odd_pos) % (10^9 + 7).n = 1
5^1 = 5.
Result: 5.n = 4
5^2 = 25.4^2 = 16.25 * 16 = 400.
Result: 400.pow() or a loop, which will overflow or time out. You must use a custom modular exponentiation function.Modular exponentiation is a "top 10" algorithm for competitive programming and technical interviews. Practice implementing pow(base, exp, mod) recursively or iteratively.