Select Log Records Meeting Severity Criteria
Arrays & HashingAuto-graded · 17 test casesLast updated
Tests understanding of ordinal comparison over structured fields versus naive text matching and proper handling of malformed input.
You are building a log processing pipeline that needs to retain only records meeting a minimum severity requirement.
Write a function that accepts three parameters:
severity_order: a list of unique, non-empty severity label strings ordered from least to most severecutoff: a severity label that appears inseverity_order, representing the minimum acceptable severityrecords: a list of log records, where each record is either an empty list or a two-element list[severity_label, message_text]
Your function should return a new list containing only the records that meet all of the following criteria:
- The record is a well-formed two-element list
- The severity label (first element) exactly matches one of the labels in
severity_order - The severity label's position indicates it is at least as severe as
cutoff(equal to or higher in the ordering)
Important rules:
- Records must be returned in their original order, preserving duplicates
- Return each qualifying record exactly as given, without modification
- Drop records that are empty lists
- Drop records where the severity label is an empty string or does not exactly match any label in
severity_order - A record with a qualifying severity and an empty message should be kept
- The message text (second element) is never examined when determining severity
Constraints
- 1 ≤ len(severity_order) ≤ 100
- All labels in
severity_orderare unique and non-empty cutoffis guaranteed to be inseverity_order- 0 ≤ len(records) ≤ 200,000
- Total characters across all strings ≤ 2,000,000
Examples
Example 1
Input: select_logs_by_severity(['DEBUG', 'INFO', 'WARN', 'ERROR'], 'WARN', [['INFO', 'contains WARN text'], ['WARN', 'slow'], ['ERROR', ''], [], ['', 'missing'], ['OTHER', 'unknown']])
Output: [['WARN', 'slow'], ['ERROR', '']]
Example 2
Input: select_logs_by_severity(['TRACE', 'DEBUG', 'INFO', 'WARN'], 'INFO', [['TRACE', 'a'], ['INFO', 'b'], ['WARN', 'c'], ['DEBUG', 'd']])
Output: [['INFO', 'b'], ['WARN', 'c']]
Hints
Hint 1
The severity comparison is about position in the ordering, not alphabetical or substring matching
Hint 2
Build a lookup structure mapping each severity label to its position index for efficient comparison
Hint 3
Check record structure before attempting to access elements to avoid index errors
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is recognizing that severity comparison must be based on ordinal position in the provided ordering, not string operations like substring matching or lexicographic comparison.
Why naive approaches fail:
- Substring matching (
cutoff in str(record)) produces false positives when the message text happens to contain the cutoff word, and false negatives for more severe levels that don't contain that substring - Lexicographic comparison (
label >= cutoff) fails because alphabetical order has no relationship to severity order (e.g., "ERROR" < "WARN" alphabetically but is more severe)
Correct algorithm:
- Build a dictionary mapping each severity label to its index position in
severity_order. This gives us the numeric rank for each label. - Look up the cutoff's rank
- Iterate through records once, keeping only those where:
- The record has exactly two elements (well-formed)
- The severity label exists in our rank dictionary (known label)
- The label's rank is greater than or equal to the cutoff rank (meets threshold)
- Append each qualifying record unchanged to the result list
This approach correctly handles all edge cases: empty records fail the length check, empty or unknown labels fail the dictionary lookup, and the comparison is purely ordinal.
Why it's correct:
- Dictionary membership is exact string matching, preventing partial matches
- The
>=comparison includes the cutoff level itself (inclusive threshold) - We never inspect the message field, so message content cannot influence filtering
- A single linear pass preserves order and duplicates naturally
Complexity
Time: O(S + N) where S is the number of severity labels (≤ 100) and N is the number of records (≤ 200,000). Building the rank dictionary takes O(S), and we process each record once with O(1) dictionary lookups. String hashing is proportional to string length, bounded by the total character limit.
Space: O(S) for the severity rank dictionary, plus O(K) for the output list containing K qualifying records. The records themselves are references, not copies.
Solution
from typing import List
def select_logs_by_severity(severity_order: List[str], cutoff: str, records: List[List[str]]) -> List[List[str]]:
severity_rank = {label: idx for idx, label in enumerate(severity_order)}
cutoff_rank = severity_rank[cutoff]
filtered = []
for record in records:
if len(record) != 2:
continue
label = record[0]
if label in severity_rank and severity_rank[label] >= cutoff_rank:
filtered.append(record)
return filtered