PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's understanding of interval merging and range manipulation, including handling overlapping and adjacent ranges, edge cases like single-point gaps and extreme values, and the ability to analyze time and space complexity.

  • Medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Merge overlapping and adjacent ranges

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

Given an unsorted list of integer ranges represented as half-open intervals [start, end) with start < end, merge all overlapping or directly adjacent ranges and return the merged list sorted by start. Also return the total length covered by the merged ranges. For example, merging [1, 4), [3, 5), and [5, 7) yields [1, 7) and total length 6. Implement an efficient solution and analyze time and space complexity. Consider edge cases such as single-point gaps, negative values, very large inputs, and stability of the original ordering when ranges are identical.

Quick Answer: This question evaluates a candidate's understanding of interval merging and range manipulation, including handling overlapping and adjacent ranges, edge cases like single-point gaps and extreme values, and the ability to analyze time and space complexity.

Merge unsorted half-open ranges that overlap or directly touch, and return merged ranges plus total covered length.

Constraints

  • Inputs are provided as Python literals matching the function signature.
  • Return a deterministic exact-match result.

Examples

Input: ([[1,4],[3,5],[5,7]],)

Expected Output: {'ranges': [[1, 7]], 'totalLength': 6}

Explanation: Prompt example.

Input: ([[10,12],[1,2],[3,4]],)

Expected Output: {'ranges': [[1, 2], [3, 4], [10, 12]], 'totalLength': 4}

Explanation: Single-point gap does not merge.

Input: ([],)

Expected Output: {'ranges': [], 'totalLength': 0}

Explanation: No ranges.

Hints

  1. Choose a representation that makes the core operation simple.
  2. Handle empty and boundary inputs before the main algorithm.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Top-p (Nucleus) Sampling in NumPy - Amazon (medium)
  • Implement Multi-Head Attention from Scratch in NumPy - Amazon (medium)
  • Detect and Break a Cycle in a Singly Linked List - Amazon (medium)
  • Caesar Cipher with Translation-Table Optimization - Amazon (medium)
  • Minimum Drone Delivery Time on a Ring of Hubs - Amazon (medium)