Longest Event Prefix Where Dropping One Event Equalizes Frequencies
Last updated: November 29, 2026
Quick Overview
Given a sequence of event type IDs, return the length of the longest prefix from which removing exactly one event leaves every remaining event type with the same count.
0
0
0 solved
Given a sequence of event type IDs, return the length of the longest prefix from which removing exactly one event leaves every remaining event type with the same count.
A Google onsite hashing problem framed around balancing sampled event types in a telemetry stream. It tests whether you can maintain two levels of counts (count per type, and number of types per count) and enumerate the few valid shapes in O(1) per step.
What the Interviewer Expects
- Maintains count per type and frequency of each count
- Tracks the maximum count incrementally
- Enumerates the valid shapes: all ones, one extra at max, one singleton
- Checks each prefix in O(1)
- Validates against a brute force on small cases
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the event that should be dropped for the longest prefix.
- Allow removing up to two events; what shapes become valid?
- Answer the same question for every suffix too.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list
eventsof hashable IDs, length up to 10^5. - Output: the maximum length L such that
events[:L]becomes uniform in counts after removing exactly one element. - Removing the only copy ...
High-Level Design
Keep cnt[x], the count of type x, and freq[c], how many types have count c, plus maxf, the largest count. After processing a prefix of length L = i + 1, it is valid if one of these shapes holds:...