Implement Prefix Match Filter
Company: Datadog
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates understanding of prefix-based string matching, data structure design (hash-based sets and trie/prefix tree), and algorithmic competencies including efficient in-memory storage and deduplication of matches.
Part 1: Hash-Based Prefix Match Filter
Constraints
- 0 <= len(prefixes), len(strings) <= 2000
- Each string length is between 0 and 200
- Strings contain lowercase English letters and may be empty
- A matching string should appear only once in the output even if it appears multiple times in the input
Examples
Input: (['ab', 'cat', 'pre'], ['abc', 'dog', 'prefix', 'cater', 'zoo'])
Expected Output: ['abc', 'prefix', 'cater']
Explanation: 'abc' starts with 'ab', 'prefix' starts with 'pre', and 'cater' starts with 'cat'.
Input: (['a', 'ab'], ['ab', 'abacus', 'ab', 'b'])
Expected Output: ['ab', 'abacus']
Explanation: Both 'ab' and 'abacus' match. The second 'ab' is omitted because output strings must be unique.
Input: ([''], ['', 'x', 'abc', 'x'])
Expected Output: ['', 'x', 'abc']
Explanation: The empty prefix matches every string, and duplicates in the input are returned once.