Implement substring search and weighted sampling
Company: Google
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithm design and analysis skills across string processing (efficient substring search) and randomized/data-structure techniques (weighted random sampling with update considerations), testing time/space complexity reasoning and probabilistic thinking for large-scale inputs.
Part 1: First Occurrence of a Pattern in Text
Constraints
- 0 <= len(text) <= 200000
- 0 <= len(pattern) <= 100000
- Strings may contain any characters
- Return 0 when pattern is empty
Examples
Input: ("abracadabra", "cada")
Expected Output: 4
Explanation: `cada` first appears in `abracadabra` starting at index 4.
Input: ("aaaaa", "bba")
Expected Output: -1
Explanation: The pattern does not occur in the text.
Input: ('', 'a')
Expected Output: -1
Explanation: A non-empty pattern cannot be found in an empty text.
Input: ("abc", "")
Expected Output: 0
Explanation: By definition, the empty pattern matches at index 0.
Input: ("mississippi", "issip")
Expected Output: 4
Explanation: `issip` starts at index 4.
Hints
- Think about how to reuse information from previous character matches instead of restarting the pattern from the beginning every time.