Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Range Index and Binary Search
00:00
5 left

Range Index and Binary Search

MediumPython

Problem

Create a range index and search it using binary search.

Implement create_range_index_and_search(ranges, queries). Each range is an inclusive [start, end] pair, ranges are non-overlapping but may be unsorted, and each query must return the original range index containing it or -1 when no range matches.

Input shapes are ranges: list[list[int]] and queries: list[int]; return list[int] in query order.

Constraints

  • 1 <= len(ranges) <= 10^5
  • 0 <= len(queries) <= 10^5
  • Each range has exactly two integers, start <= end
  • Ranges are pairwise non-overlapping
  • -10^9 <= start <= end <= 10^9
  • -10^9 <= query <= 10^9

Function Signature

def create_range_index_and_search(ranges, queries):
Interviewer

Your question is Range Index and Binary Search. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.