Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Intervals

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Intervals

Insert Interval

intermediatelayer I10 · Intervals5 tests

Given a list of non-overlapping intervals already sorted by start, insert newInterval and return the list still sorted and still non-overlapping.

  • Merge the new interval with any it overlaps or touches
  • The result must remain sorted by start
  • Inserting into an empty list yields a list holding just the new interval
  • Because the input is already sorted, this can be done in a single O(n) pass with no re-sorting

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen