Count Valid Badge-In Records Over N Days With Absence Limits

Last updated: November 29, 2026

Quick Overview

Each workday an employee's badge record is Present, Late, or Absent. A record is valid if it has fewer than two Absent days in total and never three Late days in a row. Count valid records of length n modulo 1,000,000,007.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Hard

0

0

0 solved


Each workday an employee's badge record is Present, Late, or Absent. A record is valid if it has fewer than two Absent days in total and never three Late days in a row. Count valid records of length n modulo 1,000,000,007.

A Google online-assessment hard that rewards a small state machine over (absences so far, trailing late streak). Graders check that n up to 10^5 runs fast and that every transition is accounted for.

What the Interviewer Expects
  • Choose states (absences used, current late streak)
  • Write transitions for P, L, and A
  • Use six rolling counters with a modulus
  • Sum all states at the end
  • Verify against brute force for small n
Key Topics to Cover
1D dynamic programming
State machine DP
Counting strings
Matrix exponentiation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Allow up to m absences and no k consecutive lates.
  • n is 10^18. How do you compute the answer?
  • Count only records with exactly one absence.
  • Return how many valid records end with a Late day.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: integer n >= 0. Output the number of strings of length n over {P, L, A} with at most one A and no substring LLL, mod 109+710^9+7. n = 0 counts the empty record as 1....

High-Level Design

State (a, l): a in {0, 1} absences used, l in {0, 1, 2} trailing Lates. Transitions per day: P resets the streak: (a, l) -> (a, 0). L extends it if allowed: (a, l) -> (a, l + 1) when l < 2. A us...


Submit Your Answer
Markdown supported