In the Distribute Candies interview question, you are given an array of integers where each integer represents a candy's type. You need to distribute these candies equally between a brother and a sister. However, the goal is to find the maximum number of unique types of candies the sister can receive. For example, if there are 6 candies of types [1, 1, 2, 2, 3, 3], the sister gets 3 candies, and the maximum unique types she can get is 3.
This "Easy" difficulty problem is popular at Meta, Google, and Bloomberg because it tests basic data structure knowledge and logical reasoning. It evaluation your understanding of Hash Tables or Sets and your ability to apply a simple mathematical constraint. It’s a "warm-up" problem that checks if you can identify that the sister's variety is limited by both the total number of unique types available and the total number of candies she is allowed to have.
This problem uses the Hash Set interview pattern.
Set.n / 2.Candies: [1, 1, 2, 3] ()
Whenever a problem asks for the "number of unique elements," your first thought should be a Hash Set. In many "Easy" problems, the solution is just a clever combination of a Set's size and a basic mathematical constraint.