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

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

An attendance record for a student can be represented as a string where each character says whether the student was absent, late or present on that day. The record contains only these three characters: - `'A'`: absent - `'L'`: late - `'P'`: present A student is eligible for an attendance award when both of these hold: - The student was absent (`'A'`) on strictly fewer than 2 days in total. - The student was never late (`'L'`) on 3 or more consecutive days. Given an integer `n`, return the number of possible attendance records of length `n` that make a student eligible for the award. The answer can be very large, so return it modulo `10^9 + 7`. ### Function Signature ```python def count_eligible_records(n: int) -> int: ``` ### Rules - Count distinct strings. Two records are different when they differ on at least one day. - Return the count modulo `1_000_000_007`. ### Constraints - `1 <= n <= 10^5` ### Examples ```text Input: n = 2 Output: 8 ``` There are 9 records of length 2, and only "AA" fails (two absences). The 8 eligible ones are "PP", "AP", "PA", "LP", "PL", "AL", "LA" and "LL". ```text Input: n = 1 Output: 3 ``` "P", "A" and "L" are all eligible. ```text Input: n = 3 Output: 19 ``` Without an 'A', 7 of the 8 strings over 'P' and 'L' qualify (only "LLL" fails). With exactly one 'A', there are 3 places for it and 4 ways to fill the other two days, which gives 12. The total is 7 + 12 = 19.

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.

An attendance record for a student is a string in which each character describes one day. The record contains only these three characters: - `'A'`: absent - `'L'`: late - `'P'`: present A student is eligible for an attendance award when both of these hold: - The student was absent (`'A'`) on strictly fewer than 2 days in total. - The student was never late (`'L'`) on 3 or more consecutive days. Given an integer `n`, return the number of possible attendance records of length `n` that make a student eligible for the award. Count distinct strings: two records are different when they differ on at least one day. The count can be very large, so return it modulo `1_000_000_007` (10^9 + 7). The returned value always lies in `[0, 1_000_000_006]`, so it fits a 32-bit signed integer. The true count, and sums of several reduced counts, can exceed 2^31 - 1, so Java should accumulate in `long` and C++ in `long long` before reducing. ### Constraints - `1 <= n <= 10^5` ### Example 1 ```text Input: n = 2 Output: 8 ``` There are 9 records of length 2, and only "AA" fails (two absences). The 8 eligible ones are "PP", "AP", "PA", "LP", "PL", "AL", "LA" and "LL". ### Example 2 ```text Input: n = 3 Output: 19 ``` Without an 'A', 7 of the 8 strings over 'P' and 'L' qualify (only "LLL" fails). With exactly one 'A', there are 3 places for it and 4 ways to fill the other two days, which gives 12. The total is 7 + 12 = 19.

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

  1. Ask what you need to remember about a partial record to know whether appending one more day keeps it eligible.
  2. The absence rule depends on a running total, while the late rule depends only on how the record currently ends.
  3. The count grows quickly with n, so reduce modulo 10^9 + 7 as you go instead of only at the end.

Count Award-Eligible Attendance Records

hardGoogleSoftware Engineer

An attendance record for a student is a string in which each character describes one day. The record contains only these three characters:

  • 'A': absent
  • 'L': late
  • 'P': present

A student is eligible for an attendance award when both of these hold:

  • The student was absent ('A') on strictly fewer than 2 days in total.
  • The student was never late ('L') on 3 or more consecutive days.

Given an integer n, return the number of possible attendance records of length n that make a student eligible for the award. Count distinct strings: two records are different when they differ on at least one day. The count can be very large, so return it modulo 1_000_000_007 (10^9 + 7).

The returned value always lies in [0, 1_000_000_006], so it fits a 32-bit signed integer. The true count, and sums of several reduced counts, can exceed 2^31 - 1, so Java should accumulate in long and C++ in long long before reducing.

Constraints

  • 1 <= n <= 10^5

Example 1

Input: n = 2
Output: 8

There are 9 records of length 2, and only "AA" fails (two absences). The 8 eligible ones are "PP", "AP", "PA", "LP", "PL", "AL", "LA" and "LL".

Example 2

Input: n = 3
Output: 19

Without an 'A', 7 of the 8 strings over 'P' and 'L' qualify (only "LLL" fails). With exactly one 'A', there are 3 places for it and 4 ways to fill the other two days, which gives 12. The total is 7 + 12 = 19.

Examples

Example 1
Input
(1,)
Output
3
Notes
Minimum n: 'P', 'A' and 'L' are all eligible (source example).
Example 2
Input
(2,)
Output
8
Notes
Only 'AA' fails among the 9 two-day records, so only the absence rule binds (source example).

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def count_eligible_records(n: int) -> int:
Sample call
count_eligible_records(1)
Language
Case 1
Input
(1,)
Expected
3
Case 2
Input
(2,)
Expected
8

Browse More Questions

Show the approach

Approach

Build the record one day at a time and keep six counts, one per state: the number of absences used so far (0 or 1) and the length of the trailing run of 'L' (0, 1 or 2). Start from the empty record in state (0 absences, run 0). Appending 'P' moves every state with a absences to (a, 0). Appending 'L' moves (a, l) to (a, l + 1) only when l < 2, so no record ever reaches three consecutive late days. Appending 'A' moves every state with 0 absences to (1, 0) and is never allowed from a state that already holds one absence. So the new (0, 0) count is the sum of the 0-absence counts, the new (1, 0) count is the sum of all six counts, and each run-1 or run-2 count is the previous count one run shorter. Invariant: after k steps the six counts partition the eligible records of length k by state. Both rules are closed under taking prefixes, so every eligible record of length k comes from exactly one eligible record of length k - 1 by exactly one appended character, and the update counts each one once. After n steps the sum of the six counts is the answer. Reducing every count modulo 10^9 + 7 at each step keeps the numbers small without changing the final residue; the largest intermediate sum stays below 6 * (10^9 + 7), which needs 64-bit integers in Java and C++ but is exact in JavaScript numbers. Edge cases: n = 1 gives 3 because every single-day record is eligible; n = 2 gives 8 because only "AA" fails; n = 3 is the first length where "LLL" is excluded; n = 29 is the first length whose true count exceeds the modulus.

Time complexity:
O(n)
Space complexity:
O(1)