Skip to main content
All Questions

Find the minimum height difference between a hill and any hill visible after a gap.

Unlock detailed company stats for this questionUpgrade

You are given an array of heights of hills. A hiker can see the hills ahead of him only after a certain gap distance x: if x = 2 and he is at index i, he can see hills from index i+2 onward (e.g. from i = 1 he sees 3, 4, ..., n). Find the minimum height difference between a hill and any hill visible from it.

Example: heights = [2, 4, 9, 10], gap = 2. At i = 0 he can see i = 2 and i = 3, minimum difference 7. At i = 1 he sees i = 3, minimum difference 6. From indexes 2 and 3 no hill is visible. The final answer is 6.

My ordered-set approach gave TLE after 13 of 20 test cases.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.