Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6934227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:55:58+00:00 2026-05-27T11:55:58+00:00

I came across a good question while preparing for some programming interviews. Given a

  • 0

I came across a good question while preparing for some programming interviews.

Given a set of possibly overlapping intervals, you need to write a function to return all elementary intervals among them. For example: if you are given intervals as the following list of pairs: {{1,5}, {3,10}, {5,11}, {15,18}, {16,20}}, then you need to return the following:

{{1,3}, {3,5}, {5,10}, {10,11}, {15,16}, {16,18}, {18,20}}

Note the following in the above answer:

  • The interval {11,15} is omitted in the answer because it is
    non-existent in the input.
  • The interval {1,5} from the input has been split up into {1,3}, {3,5}
    in the answer because of the starting point “3” defined in {3,10} in
    the input that cuts the interval into two elementary intervals.

Method signature in Java:

List<Pair<Integer, Integer>> generateElementaryIntervals(List<Pair<Integer, Integer> intervals)

One of the solutions that I imagined was to separate the input into non-intersecting sets, and then a simple O(NlogN) sort over all numbers in each non-intersecting set will yield the answer. Is there a more efficient way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:55:59+00:00Added an answer on May 27, 2026 at 11:55 am

    You could break this problem up into nested intervals first, then deal with each nesting separately. By nested, I mean intervals that share at least one point. For the example you gave:

    {{1,5}, {3,10}, {5,11}, {15,18}, {16,20}}
    

    there are two nestings:

    {1,5}, {3,10}, {5,11}
    

    and

    {15,18}, {16,20}
    

    In general, to determine the nestings, you can sort the intervals based on the left endpoint (as in your example), then run through and start a new nesting whenever you see {x,y}, {x',y'} with y < x'.

    For a nesting, the “elementary intervals” are formed by the sorted sequence (without repeats) of values. In the example, the nestings give

    (1,3,5,10,11) -> {1,3}, {3,5}, {5,10}, {10,11}
    

    and

    (15,16,18,20) -> {15,16}, {16,18}, {18,20}
    

    So the overall algorithm may look like this:

    • Sort the intervals based on the left endpoint
    • Run through intervals until {x,y}, {x',y'} with y < x'
    • From beginning to {x,y}, make sorted list of endpoints (no repeats), say a0,a1,...,ak
    • Add elementary intervals {ai,a(i+1)} for i = 0...k-1
    • Remove intervals up to {x,y} and continue from step 2
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am preparing for an interview and I came across these questions. Can some
While researching the issue of JSON vs XML , I came across this question
I came across one very good library for parsing CUE files. But when I
I just came across the most unexpected behavior. I'm sure there is a good
Can anybody recommend a good code profiler for C++? I came across Shiny -
In my search for a good glossary plugin for WordPress, I came across Natan
I came across a controller in an older set of code (Rails 1.2.3) that
While implementing the in-app billing for Android application, I came across a problem. Let
I came across God which seems good but I am wondering if anyone knows
Good Morning. Came across code implemented by someone else and I'm not understanding how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.