Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Range Overlap Logic
00:00
5 left

Range Overlap Logic

EasyPython

Problem

Hopper's fare search can produce two availability ranges for the same flight. Given two inclusive integer ranges, determine their overlap or return both ranges in ascending order when they are disjoint.

Formal Specification

Implement resolve_ranges(range1, range2), where each input is a two-element list [start, end] with start <= end.

  • If the ranges overlap, return the single inclusive overlap range [max(start1, start2), min(end1, end2)].
  • If they do not overlap, return the two original ranges ordered by their starting value: [[earlier_start, earlier_end], [later_start, later_end]].
  • Ranges that share an endpoint overlap at that single point. Preserve each range's values in the disjoint result.

Examples

Example 1

Input: range1 = [1, 3], range2 = [2, 7]
Output: [2, 3]

The common inclusive portion is from 2 through 3.

Example 2

Input: range1 = [8, 10], range2 = [2, 5]
Output: [[2, 5], [8, 10]]

The ranges are disjoint, so they are returned in ascending order.

Example 3

Input: range1 = [4, 6], range2 = [6, 9]
Output: [6, 6]

The ranges overlap at their shared endpoint.

Constraints

  • Each range contains exactly two integers.
  • -10^9 <= range start <= range end <= 10^9.
  • The input ranges may be given in either order.
  • Do not enumerate all integer values within a range.

Function Signature

def resolve_ranges(range1, range2):
Interviewer

Your question is Range Overlap Logic. 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.