Count attendance records of length n with fewer than two absences and no three consecutive late days

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.

Google logo
Google
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

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

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

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

Input: n = 1
Output: 3

"P", "A" and "L" are all eligible.

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.