Magicsheet logo

Can You Eat Your Favorite Candy on Your Favorite Day?

Medium
100%
Updated 8/1/2025

Asked by 1 Company

Can You Eat Your Favorite Candy on Your Favorite Day?

What is this problem about?

The Can You Eat Your Favorite Candy on Your Favorite Day? interview question is a range-overlap problem. You have several types of candies with different quantities. You eat at least 1 candy per day and at most dailyCap candies. For each query [type, day, cap], you need to determine if it's possible to eat candy of that type on that specific day. This Can You Eat Your Favorite Candy on Your Favorite Day? coding problem requires understanding the minimum and maximum candies eaten by a certain day.

Why is this asked in interviews?

Fleetx uses this to test a candidate's ability to handle large numerical ranges and prefix sums. It requires you to translate a "day" into a "total candy count" range. It tests whether you can avoid a simulation (which would be O(N) per query) and instead use O(1) range checks.

Algorithmic pattern used

This problem utilizes the Array, Prefix Sum interview pattern.

  1. Precompute the prefix sum of candy counts to know how many total candies exist before and including each type.
  2. For a query, calculate:
    • min_candies_needed: day + 1 (eating 1 candy per day).
    • max_candies_possible: (day + 1) * cap.
  3. Check if the range of candies for the favoriteType overlaps with the range [min_candies_needed, max_candies_possible].

Example explanation

Candy types: [7, 4, 5, 3]. Query: [1, 2, 2] (Type 1 on Day 2 with cap 2).

  • Type 1 range: From candy 8 to 11 (after 7 of Type 0).
  • By Day 2 (3rd day), minimum candies eaten = 3.
  • By Day 2, maximum candies eaten = 3 * 2 = 6.
  • Our eating range is [3, 6].
  • Our target type is candies [8, 11].
  • Since these ranges don't overlap, we can't reach Type 1 by Day 2. Result: False.

Common mistakes candidates make

  • Integer overflow: Since candies and days can be very large, (day + 1) * cap can easily exceed a 32-bit integer. Use long in Java or C++.
  • Off-by-one errors: Forgetting that days are 0-indexed, so Day 2 is actually the 3rd day of eating.
  • Incorrect range boundaries: Not correctly identifying the first and last candy indices for a specific type.

Interview preparation tip

Whenever a problem involves "total amount by index i," precompute a Prefix Sum. It turns range-sum queries into O(1) operations, which is essential for passing problems with many queries.

Similar Questions