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
Coding & Algorithms
Machine Learning Engineer
Confluent
March 25, 2026
Machine Learning Engineer
Take-home Project
Coding & Algorithms
Hard

29

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
Sorting and searching
Edge cases and input validation
Tree structures and recursion
Time and space complexity analysis
Data structure selection and trade-offs
Binary search and divide and conquer
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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
  1. 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.

  2. **Initializ...


Submit Your Answer
Markdown supported

Related Questions