Magicsheet logo

Student Attendance Record I

Easy
12.5%
Updated 8/1/2025

Asked by 2 Companies

Topics

Student Attendance Record I

What is this problem about?

In "Student Attendance Record I," you are given a string representing a student's attendance for a semester. The characters are 'A' (Absent), 'L' (Late), and 'P' (Present). A student is eligible for an attendance award if they meet two conditions:

  1. They were absent ('A') strictly fewer than 2 times.
  2. They were never late ('L') for 3 or more consecutive days. You need to return true if the student is eligible, and false otherwise. It is a straightforward string-checking problem.

Why is this asked in interviews?

Google and Bloomberg use this as an introductory question for early-career roles or as a quick coding warm-up. It tests basic string iteration, counting, and the ability to track "consecutive" states. While simple, it requires the candidate to correctly handle the logic of "consecutive" vs. "total" counts. It's a test of whether you can write clean, readable code for a set of clear business rules.

Algorithmic pattern used

The pattern used is a simple Linear Scan (O(n) time). You iterate through the string once, keeping a counter for the total number of 'A's and another counter for the current run of consecutive 'L's. If the 'A' count reaches 2, or the 'L' run reaches 3, you can immediately return false. If you finish the string without violating the rules, you return true.

Example explanation (use your own example)

Attendance: "PPALLL"

  1. 'P' -> OK.
  2. 'P' -> OK.
  3. 'A' -> Absent count = 1. OK.
  4. 'L' -> Late run = 1. OK.
  5. 'L' -> Late run = 2. OK.
  6. 'L' -> Late run = 3. NOT OK. The student is not eligible because they were late 3 times in a row. Attendance: "PAPA"
  7. 'P' -> OK.
  8. 'A' -> Absent count = 1. OK.
  9. 'P' -> OK.
  10. 'A' -> Absent count = 2. NOT OK. The student is not eligible because they were absent 2 times total.

Common mistakes candidates make

A common mistake is not resetting the "consecutive late" counter when a character other than 'L' is encountered. Another mistake is using a slow string search method (like searching for "LLL" and then counting 'A's separately) when a single pass is more efficient. Some candidates might also misread the requirements, such as checking for 2 consecutive absences instead of 2 total absences.

Interview preparation tip

For the Student Attendance Record I interview question, focus on writing the most concise and efficient code possible. A single loop with simple if conditions is best. Mention to your interviewer that you are using O(1) extra space and O(n) time, which is the optimal complexity for this problem.

Similar Questions