The Maximum Fruits Harvested After at Most K Steps interview question places you on a number line where fruits are positioned at various integer coordinates. You start at a given position and can move at most K steps in total — either left, right, or a combination of both. The goal is to collect the maximum number of fruits possible within that movement budget. Each fruit position has a count associated with it, and you want to maximize the total count harvested.
This problem elegantly combines spatial reasoning with optimization, making it a compelling Maximum Fruits Harvested coding problem for competitive programmers and interview candidates alike.
Top companies like Microsoft, Amazon, and Deutsche Bank use this problem to test candidates' ability to think about range queries and movement constraints simultaneously. It evaluates whether you can bridge the gap between a physical intuition (moving on a line) and an algorithmic pattern (sliding window over sorted positions). Candidates who can translate movement constraints into window bounds demonstrate strong problem modeling skills.
This problem uses a combination of sliding window and prefix sums over sorted fruit positions. The key insight is: if you move right by r steps and then backtrack left, or move left by l steps then right, the total movement is bounded by K. You enumerate over how far you go in one direction, then binary search or slide a window to find the farthest valid position in the other direction. A prefix sum array over sorted positions lets you calculate total fruits in any range in O(1), making the overall algorithm O(n log n) or O(n) depending on implementation.
Suppose fruits are at positions: [(2, 3), (5, 5), (7, 2), (9, 4)] (position, count), you start at position 4, and K = 5.
Best option here is going purely right: 11 fruits. The sliding window helps you enumerate these cases systematically.
right_distance + 2 * left_distance or 2 * right_distance + left_distance, depending on direction. Getting this formula wrong leads to incorrect window bounds.Practice this Array Sliding Window Binary Search Prefix Sum interview pattern by first solving simpler "reach all positions within K steps" problems. Then layer in the "collect items" aspect. When you see movement on a 1D line with a step budget, immediately think about the two-direction split formula: min(left, right) * 2 + max(left, right) ≤ K. This insight unlocks the window logic cleanly.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Count Subarrays With Score Less Than K | Hard | Solve | |
| Minimum Size Subarray Sum | Medium | Solve | |
| Max Consecutive Ones III | Medium | Solve | |
| Subarray Product Less Than K | Medium | Solve | |
| Apply Operations to Maximize Frequency Score | Hard | Solve |