The Eat Pizzas! interview question involves n pizzas, each with a specific weight. You want to eat all pizzas over several days. Each day, you pick exactly 4 pizzas. On odd-numbered days (1, 3, 5...), you get to eat the heaviest pizza of the 4. On even-numbered days (2, 4, 6...), you get to eat the second-heaviest pizza of the 4. You need to determine the maximum total weight of pizzas you can eat.
Infosys and similar companies use this greedy interview pattern to test a candidate's ability to optimize a selection process under specific constraints. It requires sorting and a clear strategy for how to "pair" pizzas each day to maximize your gain. It evaluates whether you can derive the mathematical indices for the elements you should pick from a sorted array.
This problem is solved using Sorting and a Greedy approach.
n / 4.odd_days be the number of odd-numbered days and even_days be the number of even-numbered days.odd_days heaviest pizzas from the end of the array.Suppose n = 8 pizzas (2 days). Weights: [1, 2, 3, 4, 5, 6, 7, 8].
[1, 2, 3, 4, 5, 6, 7, 8].[1, 2, 3, 4, 5] are just filler.
Total Weight: 8 + 6 = 14.Whenever you need to maximize a sum of "best" elements under grouping rules, sorting is the first step. Think about which elements are "wasted" and which are "collected" to find the optimal indices.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Number of Consecutive Values You Can Make | Medium | Solve | |
| Array With Elements Not Equal to Average of Neighbors | Medium | Solve | |
| Destroying Asteroids | Medium | Solve | |
| Divide Array Into Arrays With Max Difference | Medium | Solve | |
| Eliminate Maximum Number of Monsters | Medium | Solve |