Find shortest distance in interval list
Last updated: March 25, 2026
Quick Overview
Given a list of intervals, write a function to find the shortest distance between any two intervals that do not overlap. The function should take a list of intervals as input, where each interval is represented as a pair of integers [start, end], and return the minimum distance as an integer. If all intervals overlap, return -1.
Confluent
March 25, 202629
1
1,808 solved
Given a list of intervals, write a function to find the shortest distance between any two intervals that do not overlap. The function should take a list of intervals as input, where each interval is represented as a pair of integers [start, end], and return the minimum distance as an integer. If all intervals overlap, return -1.
This coding problem is frequently asked during Take-home Project at Confluent. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Confluent expects candidates to write production-quality code, not just solve the puzzle.
What the Interviewer Expects
- Quickly identify the optimal approach and its theoretical basis
- Handle complex algorithm design with multiple interacting components
- Write concise, elegant code under time pressure
- Prove correctness of your approach and discuss alternative solutions
- Optimize beyond the obvious: discuss constant factor improvements
- Address follow-up variations and explain how the solution generalizes
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if the input doesn't fit in memory?
- How would you test this solution thoroughly?
- How would your solution change if the input was sorted?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
To find the shortest distance between non-overlapping intervals, we can leverage sorting and a linear scan. The two-pointer technique applies here since we can keep track of two intervals—one for the ...
Approach
-
Sort the Intervals: Begin by sorting the list of intervals based on their start times. This allows us to compare only consecutive intervals for potential overlaps and distances.
-
**Initializ...
Submit Your Answer
Confluent Machine Learning Engineer Interview Guide
Interview process, tips, and preparation timeline