Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.