Longest Prefix Where Removing One Element Equalizes All Frequencies

Quick Overview

Find the longest prefix of an integer array from which removing exactly one element leaves every remaining value with the same number of occurrences. Tests frequency-of-frequency counting and a careful case analysis that checks each prefix in constant time.

Longest Prefix Where Removing One Element Equalizes All Frequencies

Company: Activision Blizzard

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given an array `nums` of positive integers, return the longest possible length of a prefix of `nums` such that it is possible to remove exactly one element from this prefix so that every number that has appeared in it has the same number of occurrences. If no elements remain after the removal, the prefix still counts as valid: every number that appeared is considered to have the same number of occurrences (0). ### Function Signature ```python def max_equal_frequency(nums: list[int]) -> int: ... ``` ### Rules - A prefix is `nums[0:k]` for some `k` from 1 to `len(nums)`. Return that `k`, the length before the removal. - Exactly one element must be removed. A prefix whose counts are already all equal is valid only if some single removal keeps them equal, so `[1, 1, 2, 2]` is not valid. - A number whose only occurrence is removed no longer appears in the prefix, so it does not have to match the others. ### Constraints - `2 <= len(nums) <= 10^5` - `1 <= nums[i] <= 10^5` ### Examples ```text Input: nums = [2, 2, 1, 1, 5, 3, 3, 5] Output: 7 ``` For the prefix of length 7, `[2, 2, 1, 1, 5, 3, 3]`, removing the 5 leaves 1, 2 and 3 with two occurrences each. The full array has every value exactly twice, and any single removal leaves one value at 1 and the rest at 2, so length 8 fails. ```text Input: nums = [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5] Output: 13 ``` The whole array works. Removing the 5 leaves 1, 2, 3 and 4 with three occurrences each. ```text Input: nums = [1, 1, 1, 2, 2, 2] Output: 5 ``` In the full array both values occur 3 times, and removing any element leaves counts 3 and 2. The prefix `[1, 1, 1, 2, 2]` works by removing one 1, which leaves both values at 2.

Overview: Find the longest prefix of an integer array from which removing exactly one element leaves every remaining value with the same number of occurrences. Tests frequency-of-frequency counting and a careful case analysis that checks each prefix in constant time.

Activision Blizzard logo
Activision Blizzard
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given an array nums of positive integers, return the longest possible length of a prefix of nums such that it is possible to remove exactly one element from this prefix so that every number that has appeared in it has the same number of occurrences.

If no elements remain after the removal, the prefix still counts as valid: every number that appeared is considered to have the same number of occurrences (0).

Function Signature

def max_equal_frequency(nums: list[int]) -> int:
    ...

Rules

  • A prefix is nums[0:k] for some k from 1 to len(nums) . Return that k , the length before the removal.
  • Exactly one element must be removed. A prefix whose counts are already all equal is valid only if some single removal keeps them equal, so [1, 1, 2, 2] is not valid.
  • A number whose only occurrence is removed no longer appears in the prefix, so it does not have to match the others.

Constraints

  • 2 <= len(nums) <= 10^5
  • 1 <= nums[i] <= 10^5

Examples

Input: nums = [2, 2, 1, 1, 5, 3, 3, 5]
Output: 7

For the prefix of length 7, [2, 2, 1, 1, 5, 3, 3], removing the 5 leaves 1, 2 and 3 with two occurrences each. The full array has every value exactly twice, and any single removal leaves one value at 1 and the rest at 2, so length 8 fails.

Input: nums = [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5]
Output: 13

The whole array works. Removing the 5 leaves 1, 2, 3 and 4 with three occurrences each.

Input: nums = [1, 1, 1, 2, 2, 2]
Output: 5

In the full array both values occur 3 times, and removing any element leaves counts 3 and 2. The prefix [1, 1, 1, 2, 2] works by removing one 1, which leaves both values at 2.