We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Number of Senior Citizens

Number: 2727

Difficulty: Easy

Paid? No

Companies: Google, Bloomberg


Problem Description

Given an array of strings where each string is a 15-character compressed representation of a passenger's details, determine how many passengers are strictly more than 60 years old. The structure of each string is as follows:

  • First 10 characters: phone number
  • 11th character: gender ('M', 'F', or 'O')
  • Characters 12-13: age (as a two-digit number)
  • Last 2 characters: seat number

The task is to count the number of passengers with age strictly > 60.


Key Insights

  • The age of each passenger is embedded in two characters in the string.
  • Convert the substring representing the age into an integer.
  • Simply iterate through each detail and count those with an age greater than 60.
  • String slicing and integer conversion are sufficient without additional data structures.

Space and Time Complexity

Time Complexity: O(n) where n is the number of detail strings. Space Complexity: O(1) additional space.


Solution

The solution involves iterating through each string in the provided array. For each string, extract the substring that represents the age (located at positions 11 and 12 with 0-indexing adjustments) and convert it to an integer. If the age is greater than 60, increment a counter. This approach leverages basic string operations and conditional checking, ensuring simplicity and efficiency.


Code Solutions

# Define a function to count the number of senior citizens
def countSeniors(details):
    # Initialize the counter for senior citizens
    senior_count = 0
    # Loop through each detail in the provided list
    for detail in details:
        # Extract the age substring from the detail string (characters 11-12)
        age = int(detail[11:13])
        # Check if the age is greater than 60
        if age > 60:
            # Increment the counter if the condition is met
            senior_count += 1
    # Return the total number of senior citizens
    return senior_count

# Example usage:
details = ["7868190130M7522", "5303914400F9211", "9273338290F4010"]
print(countSeniors(details))  # Output: 2
← Back to All Questions