KatInterview

Filter Names Absent From a Blocklist After Cleanup

Arrays & HashingAuto-graded · 21 test cases
EasyAuto-gradedArrays & Hashinghash-setstringsnormalization

Last updated

Return the items of one string list whose trimmed, lowercased form is missing from a second list, keeping original spelling, order, and duplicates; tests hash sets and normalization.

You are given two lists of strings: candidates and blocklist.

Two strings are considered the same if they are equal after this cleanup:

  1. Remove leading and trailing whitespace.
  2. Convert all letters to lowercase.

Return a new list containing every string from candidates whose cleaned form does not equal the cleaned form of any string in blocklist.

Rules for the output:

  • Return the strings exactly as they appear in candidates (original spacing and letter case). Cleanup is used only for comparison.
  • Keep the original relative order of candidates.
  • Do not remove duplicates: if several kept entries are identical (or equivalent after cleanup), every one of them appears in the result.
  • Repeated entries in blocklist have no extra effect.
  • If candidates is empty, the result is an empty list. If blocklist is empty, every entry of candidates is kept.
  • A string consisting only of whitespace cleans to the empty string, and is removed only if blocklist contains an entry that also cleans to the empty string.

Constraints

  • 0 <= len(candidates), len(blocklist) <= 10^5
  • Each string has length at most 100.
  • Use Python's strip() and lower() semantics for the cleanup.
  • Aim for roughly linear time in the total size of the input.

Examples

Example 1

Input: filter_blocked_names([' Apple ', 'BANANA', 'apple', 'Cherry'], ['apple', ' banana '])

Output: ['Cherry']

Example 2

Input: filter_blocked_names(['One', 'Two', 'Three'], [' four ', 'TWO'])

Output: ['One', 'Three']

Example 3

Input: filter_blocked_names([], ['x'])

Output: []

Hints

Hint 1

Cleanup should be treated as a comparison key only; the output must contain the original, untouched strings.

Hint 2

Checking membership in a list for each candidate is quadratic. Pre-compute the cleaned forms of the blocklist once and store them in a hash set.

Hint 3

Make one pass over the candidates, clean each one, and keep it if its key is not in the set. This automatically preserves order and duplicates.