KatInterview

Select Log Records Meeting Severity Criteria

Arrays & HashingAuto-graded · 17 test cases
MediumAuto-gradedArrays & Hashingfilteringstring-handlingedge-cases

Last 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:

  1. severity_order: a list of unique, non-empty severity label strings ordered from least to most severe
  2. cutoff: a severity label that appears in severity_order, representing the minimum acceptable severity
  3. records: 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_order are unique and non-empty
  • cutoff is guaranteed to be in severity_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