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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

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
Hash Map
Frequency of Frequencies
Counting
Prefixes
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: list events of 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:...


Submit Your Answer
Markdown supported