Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed array of strings details. Each element of details provides information about a given passenger compressed into a string of length 15. The system is such that:
Return the number of passengers who are strictly more than 60 years old.
Example 1:
Input: details = ["7868190130M7522","5303914400F9211","9273338290F4010"] Output: 2 Explanation: 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 Explanation: None of the passengers are older than 60.
Constraints:
1 <= details.length <= 100details[i].length == 15details[i] consists of digits from '0' to '9'.details[i][10] is either 'M' or 'F' or 'O'.Problem summary: You are given a 0-indexed array of strings details. Each element of details provides information about a given passenger compressed into a string of length 15. The system is such that: The first ten characters consist of the phone number of passengers. The next character denotes the gender of the person. The following two characters are used to indicate the age of the person. The last two characters determine the seat allotted to that person. Return the number of passengers who are strictly more than 60 years old.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
["7868190130M7522","5303914400F9211","9273338290F4010"]
["1313579440F2036","2921522980M5644"]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2678: Number of Senior Citizens
class Solution {
public int countSeniors(String[] details) {
int ans = 0;
for (var x : details) {
int age = Integer.parseInt(x.substring(11, 13));
if (age > 60) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2678: Number of Senior Citizens
func countSeniors(details []string) (ans int) {
for _, x := range details {
age, _ := strconv.Atoi(x[11:13])
if age > 60 {
ans++
}
}
return
}
# Accepted solution for LeetCode #2678: Number of Senior Citizens
class Solution:
def countSeniors(self, details: List[str]) -> int:
return sum(int(x[11:13]) > 60 for x in details)
// Accepted solution for LeetCode #2678: Number of Senior Citizens
impl Solution {
pub fn count_seniors(details: Vec<String>) -> i32 {
details
.iter()
.filter_map(|s| s[11..13].parse::<i32>().ok())
.filter(|&age| age > 60)
.count() as i32
}
}
// Accepted solution for LeetCode #2678: Number of Senior Citizens
function countSeniors(details: string[]): number {
return details.filter(x => +x.slice(11, 13) > 60).length;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.