r/Hack2Hire • • Oct 20 '26

OA IBM OA: Maximum Distinct Split Sum

Problem An analytics system measures how diverse the values are on both sides of an array split. Given an integer array arr, split it at an index i with 1 ≤ i < arr.length into two non-empty parts, arr[0...i-1] and arr[i...end]. The score of a split is the number of distinct integers in the left part plus the number of distinct integers in the right part.

Return the maximum score over all valid splits. The solution must run in O(N) time, where N is the length of arr. This is a variation of LeetCode 1525, Number of Good Ways to Split a String.

Constraints: 2 ≤ arr.length ≤ 105 and -109 ≤ arr[i] ≤ 109.

Follow-up The backend lists these follow-up directions for this question: - How would you change the algorithm to return every split index that achieves the maximum score? - Could you reduce the auxiliary space while keeping O(N) time, for example by using value ranges or sorting?

Example Input: arr = [1, 2, 1, 3, 2] Output: 5

Explanation: - Splitting at index 2 gives [1, 2] and [1, 3, 2]. - The left side has 2 distinct values and the right side has 3, so the score is 5.

Two more cases: [4, 1, 4, 2, 3, 2, 5, 1] returns 7, and [5, 5] returns 2.


Suggested Approach

  1. Start with every value in the right part and count occurrences in a frequency map. The number of keys is the distinct count on the right.
  2. Move the split one position at a time. Each step moves one value from the right part to the left part: add it to a set for the left side, decrement its right-side frequency, and delete the key only when the frequency hits zero.
  3. After each move, the set size and the map size are the two distinct counts for that split. Add them and keep the best score.

Time & Space Complexity

  • Time: O(N), since each element is counted once and moved across the split once.
  • Space: O(N) for the distinct values and their remaining frequencies.

Targeting IBM interviews? We track their most-asked question patterns at Hack2Hire. Practice this question here → Practice Question Here

Join the community to see more interview experiences from real candidates → Hack2Hire Forum


Compiled from publicly available platforms and community-shared experiences.

8 Upvotes

0 comments sorted by