Convert State Stream to Events
Company: Anthropic
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's ability to process sequential categorical data by identifying and summarizing consecutive runs, reason about time and space complexity, and apply length-based filtering of events.
Part 1: Convert State Stream into Events
Constraints
- 0 <= len(states) <= 100000
- Each element in `states` supports equality comparison
- Indices are 0-based
- Runs are maximal: two adjacent events must always have different values
Examples
Input: (['A', 'A', 'B', 'B', 'B', 'A'],)
Expected Output: [('A', 0, 1), ('B', 2, 4), ('A', 5, 5)]
Explanation: Three runs appear: A from 0 to 1, B from 2 to 4, and A from 5 to 5.
Input: ([],)
Expected Output: []
Explanation: An empty stream has no events.
Input: (['X'],)
Expected Output: [('X', 0, 0)]
Explanation: A single element forms one event of length 1.
Input: ([1, 2, 2, 3, 3, 3, 2],)
Expected Output: [(1, 0, 0), (2, 1, 2), (3, 3, 5), (2, 6, 6)]
Explanation: Each maximal run is converted into one tuple.