Count attendance records of length n with fewer than two absences and no three consecutive late days
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Other
Overview: Count the attendance records of length n, built from absent, late and present days, that have fewer than two absences and never three late days in a row, modulo a large prime. It tests dynamic programming over a small state space, a clean correctness argument, and modular arithmetic for n up to 100,000.
Constraints
- 1 <= n <= 10^5
- Return the count modulo 1_000_000_007 (10^9 + 7); the result lies in [0, 1_000_000_006] and fits a 32-bit signed integer.
- Intermediate sums of reduced counts can exceed 2^31 - 1, so Java should use long and C++ long long while accumulating.
Examples
Input: (1,)
Expected Output: 3
Explanation: Minimum n: 'P', 'A' and 'L' are all eligible (source example).
Input: (2,)
Expected Output: 8
Explanation: Only 'AA' fails among the 9 two-day records, so only the absence rule binds (source example).
Hints
- Ask what you need to remember about a partial record to know whether appending one more day keeps it eligible.
- The absence rule depends on a running total, while the late rule depends only on how the record currently ends.
- The count grows quickly with n, so reduce modulo 10^9 + 7 as you go instead of only at the end.