Count kth largest element in interval list

Last updated: April 1, 2026

Quick Overview

Given a list of intervals, each defined by a pair of integers representing its start and end points, write a function to determine the k-th largest interval based on its length. The function should take in a list of intervals and an integer k, and return the length of the k-th largest interval. If k is greater than the number of intervals, return -1.

Compass
Coding & Algorithms
Software Engineer
Compass
April 1, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

14

3,059 solved


Given a list of intervals, each defined by a pair of integers representing its start and end points, write a function to determine the k-th largest interval based on its length. The function should take in a list of intervals and an integer k, and return the length of the k-th largest interval. If k is greater than the number of intervals, return -1.

This coding problem is frequently asked during Phone Screen at Compass. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Compass expects candidates to write production-quality code, not just solve the puzzle.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Hash maps and frequency counting
Dynamic programming and memoization
Binary search and divide and conquer
Time and space complexity analysis
Sorting and searching
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
  • How would you test this solution thoroughly?
  • What happens if the input contains duplicates?
  • What is the worst-case input for your solution?
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

The problem requires us to find the k-th largest interval based on its length from a list of intervals. The specific pattern that applies here is sorting, as we need to compare the lengths of the inte...

Approach
  1. First, calculate the lengths of each interval by subtracting the start from the end point. For example, for the intervals [(1, 4), (2, 6), (5, 8)], the lengths would be [3, 4, 3].
  2. Next, sort t...

Submit Your Answer
Markdown supported

Related Questions