Minimize the Longest Hike Between Rest Stops
Reported by candidates from Pinterest's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills a naive solution on this Pinterest OA, reported in August 2026, is restStops equal to segments.length - 1, where every segment stands alone and the answer is just the max element. This is the classic split array largest sum problem dressed up as a hike. You'll see it as dynamic programming on paper, but the clean solution is a binary search on the answer with a greedy check. With 100000 segments, an O(n^2 * k) DP will time out. If you blank on the approach mid-assessment, StealthCoder runs invisibly as a safety net and hands you the structure.
The problem
You are given an array segments, where segments[i] is the positive hiking time of the ith consecutive segment of one route, and an integer restStops. Place every rest stop between two adjacent segments. Using exactly restStops rest stops divides the route into exactly restStops + 1 nonempty continuous hiking groups. The duration of a group is the sum of its segment times. Return the minimum possible value of the maximum group duration. You only need to return this minimum duration; you do not need to reconstruct a placement. Function minimizeLongestHike(segments: int[], restStops: int) → long Examples Example 1 segments = [7,2,5,10,8] restStops = 1 return = 18 Place the rest stop after the third segment. The two group durations are 14 and 18. Every other placement has a maximum duration of at least 18. Example 2 segments = [4,2,7,3,6] restStops = 2 return = 9 Use groups [4,2], [7], and [3,6], whose durations are 6, 7, and 9. A maximum below 9 cannot fit these segments into three groups. Example 3 segments = [6,3,8] restStops = 2 return = 8 Every segment forms its own group, so the longest continuous hike is the longest individual segment. Constraints 1 <= segments.length <= 100000. 1 <= segments[i] <= 10^9. 0 <= restStops < segments.length. The sum of all segment times fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Binary search the answer. The lower bound is max(segments), because no group can be smaller than its biggest single segment. The upper bound is sum(segments), which is the restStops = 0 case. For a candidate limit, scan left to right, add segments to the current group, and start a new group when the next one would exceed the limit. Count groups. If the count is at most restStops + 1, the limit is feasible, so search lower. Otherwise search higher. Complexity is O(n log(sum)). The pitfalls: using int instead of 64-bit for sums, starting the low bound at 1 instead of max(segments), and off-by-one on group count versus rest stops. Exactly restStops stops forces nonempty groups, but a feasible limit with fewer groups can always be split further without raising the max. StealthCoder is the hedge if the live OA catches you mid-blank on that monotonic feasibility argument.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Minimize the Longest Hike Between Rest Stops cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as split array largest sum. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Pinterest's OA.
Pinterest reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimize the Longest Hike Between Rest Stops FAQ
What's the trick for Minimize the Longest Hike Between Rest Stops?+
Binary search on the answer. Feasibility is monotonic: if a max duration L works, any larger value works too. Check feasibility with a greedy scan that packs segments into groups until the next one would overflow L. Count the groups and compare to restStops + 1.
Do I need dynamic programming for this one?+
No. The DP works at O(n^2 * k) but that's too slow for 100000 segments. Binary search plus greedy gets O(n log(sum)). Mention the DP as the baseline if asked, then go straight to the faster approach.
What edge cases break naive solutions?+
restStops equal to length - 1 forces every segment alone, so the answer is the max element. restStops = 0 gives the total sum. A single segment also works. Also watch overflow: sums can exceed 32-bit, so use 64-bit integers for everything.
How do I set the binary search bounds?+
Low is the maximum single segment, since a group can't be shorter than its largest piece. High is the sum of all segments. Loop while low < high, compute mid, and if the greedy check passes set high = mid, otherwise low = mid + 1.
How do I prepare for this in 48 hours?+
Solve the split array largest sum pattern until the feasibility check is automatic. Then write this version from scratch with 64-bit sums. Test on the three examples plus the restStops = 0 and restStops = n - 1 cases. That covers what Pinterest reported in August 2026.