LeetCode Problem Workspace

Number of Senior Citizens

Determine how many passengers are over 60 years old based on their compressed details in an array of strings.

category

2

Topics

code_blocks

6

Code langs

hub

3

Related

Practice Focus

Easy · Array plus String

bolt

Answer-first summary

Determine how many passengers are over 60 years old based on their compressed details in an array of strings.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array plus String

Try AiBox Copilotarrow_forward

This problem requires parsing an array of 15-character strings. Each string contains a compressed passenger record, and the goal is to determine how many passengers are over 60 years old. By extracting the age from each string, you can calculate the solution in linear time.

Problem Statement

You are given an array of strings, where each string contains compressed passenger details. Each string has the format "XXXXXXXXXXXXYZZZ", where the digits represent the phone number, the 11th character is the gender, and the 12th and 13th characters together represent the passenger's age in years. Your task is to return the number of passengers who are strictly older than 60 years.

To solve this, extract the age from the relevant characters in each string, compare it to 60, and count how many passengers meet the age condition. The passenger details do not contain extra spaces, and the gender data is irrelevant for this task. Ensure the solution is efficient for up to 100 passengers.

Examples

Example 1

Input: details = ["7868190130M7522","5303914400F9211","9273338290F4010"]

Output: 2

The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.

Example 2

Input: details = ["1313579440F2036","2921522980M5644"]

Output: 0

None of the passengers are older than 60.

Constraints

  • 1 <= details.length <= 100
  • details[i].length == 15
  • details[i] consists of digits from '0' to '9'.
  • details[i][10] is either 'M' or 'F' or 'O'.
  • The phone numbers and seat numbers of the passengers are distinct.

Solution Approach

Extract and Compare Age

Extract the substring from each passenger's details representing the age (characters at positions 11 and 12). Convert this substring to an integer and compare it with 60 to count passengers who are older.

Iterate Through the Array

Loop through each passenger's details in the array. For each passenger, extract their age, perform the comparison, and increment a counter if the passenger is older than 60.

Efficiency Considerations

The problem can be solved in O(n) time, where n is the number of passengers. The space complexity is O(1) since no extra space is needed beyond the counter.

Complexity Analysis

Metric Value
Time O(n)
Space O(1)

The time complexity is O(n) because we loop through each element of the array once. The space complexity is O(1) as we use a single counter for the result, and no extra space is required for processing.

What Interviewers Usually Probe

  • The candidate should focus on efficiently parsing and comparing the age values.
  • A clear understanding of string indexing and conversion to integers is essential for solving this problem.
  • The candidate must handle edge cases, such as when no passengers are older than 60.

Common Pitfalls or Variants

Common pitfalls

  • Incorrectly handling the substring extraction for the age (indexing issues).
  • Forgetting to convert the age from a string to an integer before comparison.
  • Overcomplicating the solution by introducing unnecessary steps or data structures.

Follow-up variants

  • Modify the problem to count passengers over a different age threshold (e.g., 50 or 70).
  • Handle cases where the passenger details contain invalid or corrupted data (e.g., malformed strings).
  • Implement a more complex filtering system based on other passenger details, such as gender or seat number.

FAQ

How do I solve the 'Number of Senior Citizens' problem?

Extract the passenger's age from the 11th and 12th characters of each string, convert it to an integer, and count how many are over 60 years old.

What is the time complexity of the 'Number of Senior Citizens' problem?

The time complexity is O(n), where n is the number of passengers in the array.

Can I solve this problem without converting the age string to an integer?

No, you must convert the age substring to an integer to perform the comparison against 60.

What are common mistakes when solving the 'Number of Senior Citizens' problem?

Common mistakes include incorrect string indexing, failing to convert the age substring to an integer, and unnecessary overcomplication of the solution.

How can I handle edge cases in the 'Number of Senior Citizens' problem?

Make sure to account for edge cases like empty arrays or passengers exactly 60 years old by using the strict 'greater than' condition.

terminal

Solution

Solution 1: Traversal and Counting

We can traverse each string $x$ in `details` and convert the $12$th and $13$th characters (indexed at $11$ and $12$) of $x$ to integers, and check if they are greater than $60$. If so, we add one to the answer.

1
2
3
class Solution:
    def countSeniors(self, details: List[str]) -> int:
        return sum(int(x[11:13]) > 60 for x in details)
Number of Senior Citizens Solution: Array plus String | LeetCode #2678 Easy