KatInterview

Extract Stock Symbols from a Text Snippet

StringsAuto-graded · 18 test cases
MediumAuto-gradedStringsstringshashingtrie

Last updated

Tests whole-word, case-insensitive multi-token matching with trie-based scanning and deduplication ordered by a reference list.

Problem

You are given a text snippet and a reference list that maps entity names to short symbol codes. Your task is to scan the text for any entity names that appear as complete, consecutive word sequences, then return the corresponding symbols.

Definitions

  • Token: a maximal contiguous run of alphanumeric characters ([a-zA-Z0-9]). Every non-alphanumeric character (spaces, punctuation, symbols) acts as a separator and is otherwise ignored.
  • Match: an entity name matches the text if and only if its tokens appear as a contiguous subsequence of the text's tokens, in the same order, with no gaps. Matching is case-insensitive.

Rules

  1. Tokenize both the text and every entity name using the definition above, then lowercase all tokens before comparing.
  2. A name with multiple words must appear as consecutive tokens in the text. A partial token is never a match — for example, the name "App" does not match the token "apple".
  3. Return only unique symbols. If the same symbol is reachable through several different names, include it only once.
  4. The output order follows the reference list, not the order of appearance in the text. Specifically, iterate through the reference list from top to bottom and emit each symbol the first time it qualifies.

Constraints

  • 1 <= len(text) <= 10^5
  • 1 <= len(reference) <= 10^4
  • Each entity name contains between 1 and 10 words, each word consisting of alphanumeric characters only.
  • text contains only standard printable ASCII characters.
  • Symbols are non-empty strings of uppercase alphanumeric characters.

Examples

Example 1

Input: find_mentioned_tickers('Apple, along with Microsoft Corp., announced a partnership with OpenAI.', [['Apple', 'AAPL'], ['Amazon', 'AMZN'], ['Microsoft Corp', 'MSFT'], ['OpenAI', 'OPAI'], ['Microsoft', 'MSFT']])

Output: ['AAPL', 'MSFT', 'OPAI']

Example 2

Input: find_mentioned_tickers('Meta Platforms and meta are investing heavily in AI.', [['Meta Platforms', 'META'], ['Meta', 'META']])

Output: ['META']

Hints

Hint 1

Tokenize the text once with a regex that extracts maximal alphanumeric runs, then lowercase every token. Apply the same normalization to each entity name so both sides are comparable on equal footing.

Hint 2

Build a word-level trie from all entity names. At each terminal node store the normalized word tuple that identifies that name. Then slide a pointer across the text tokens: from each starting position, walk the trie as far as consecutive tokens allow, recording every terminal node you pass through.

Hint 3

Detection (which names appear) and output ordering (which symbols to emit) are independent concerns. After scanning, collect matched name tuples in a set, then make a single pass over the reference list to emit symbols in order, skipping duplicates with a seen set.