Reported August 2026
TikTokbinary search

Minimum Height Difference Between Distant Peaks

Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live TikTok OA. Under 2s to a working solution.
Founder's read

The viewingGap parameter is the whole problem. TikTok reported this one in August 2026, and it looks like a plain closest-pair question until you notice that only peaks at least viewingGap apart can be compared. With up to 10^5 heights, the brute-force double loop is dead on arrival. If you've got an OA invite for this, the pattern is a sorted set swept left to right. StealthCoder is there as a safety net if your mind goes blank mid-assessment, but the idea is short enough to hold in your head.

The problem

A mountaineer is studying a line of mountain peaks numbered from 0 onward. You are given their heights in meters as an integer array heights and a minimum viewing distance viewingGap.
The mountaineer may compare peaks at indices a and b only when |a - b| >= viewingGap.
Return the minimum possible value of |heights[a] - heights[b]| over all comparable pairs.

Function
minimumPeakHeightDifference(heights: int[], viewingGap: int) → int

Examples
Example 1
heights = [1,5,4,20,9]
viewingGap = 3
return = 4
The comparable index pairs are (0, 3), (0, 4), and (1, 4). Their height differences are 19, 8, and 4, so the minimum is 4.
Example 2
heights = [3,20,5,8]
viewingGap = 1
return = 2
Every pair of distinct indices is comparable. Peaks at indices 0 and 2 have heights 3 and 5, giving the minimum difference 2.
Example 3
heights = [7,12,7]
viewingGap = 2
return = 0
The only comparable pair is at indices 0 and 2. The two heights are equal, so their difference is 0.

Constraints
2 <= heights.length <= 10^5
0 <= heights[i] <= 10^9
1 <= viewingGap < heights.length

Reported by candidates. Source: FastPrep

Pattern and pitfall

Sweep index i from viewingGap to n-1. Before handling i, insert heights[i - viewingGap] into a sorted structure. That structure then holds exactly the indices that are at least viewingGap behind i. Query it for the floor and ceiling of heights[i], take the smaller absolute difference, and keep a running minimum. That's O(n log n). The common pitfall is inserting the wrong index and letting a too-close peak into the set, or checking only the floor and forgetting the ceiling. Another trap is using a plain list and scanning it, which quietly brings back O(n^2). In Python you don't get a built-in sorted set, so bisect with insort works but can be slow on inserts. If you blank on the data structure during the live OA, StealthCoder can supply the sorted-set skeleton while you check the edge cases. Exit early if the minimum hits 0.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Minimum Height Difference Between Distant Peaks 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as minimum absolute difference between elements with constraint. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass TikTok's OA.

TikTok reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Height Difference Between Distant Peaks FAQ

What's the trick in the TikTok Minimum Height Difference Between Distant Peaks problem?+

Sweep left to right and keep a sorted set of heights whose indices are at least viewingGap behind the current one. For each peak, find its nearest neighbors in that set (floor and ceiling) and update the minimum difference. That avoids checking every pair.

How hard is this problem really?+

Medium to medium-hard. The idea is simple once you see it, but you need a sorted structure with neighbor lookup. Languages without a built-in balanced tree make it harder. The reasoning is easy, and the implementation is where people lose time.

Can I just brute force it?+

No. With heights.length up to 10^5, checking all comparable pairs is roughly 5 billion operations in the worst case. It passes the small examples and times out on the large hidden tests. You need the O(n log n) sorted-set approach.

What edge cases should I test?+

Test viewingGap equal to n-1, where only one pair exists, as in the [7,12,7] example. Test duplicate heights, which should return 0 immediately. Test viewingGap of 1, where every distinct pair counts. Also test large heights near 10^9 to confirm nothing overflows.

How do I prepare for this in 48 hours?+

Write the sweep once from scratch in your language of choice. Know how to get floor and ceiling lookups, whether through bisect, TreeSet, or std::set with lower_bound. Then run the three given examples by hand. That covers the whole pattern for this kind of distance-constrained closest-pair question.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with TikTok.

OA at TikTok?
Invisible during screen share
Get it