Cse220 Quiz 2
Cse220 Quiz 2
Cse220 Quiz 2
Name: ID:
Section:
1. Given a set of sorted intervals, find all non-overlapping intervals after merging the
overlapping intervals using stack. Assume that you already have a stack
implemented that allows you to push and pop a tuple. You have to write a function
that takes the sorted intervals as input and returns merged intervals.
For example,
Input: [(1, 5), (2, 3), (4, 6), (7, 8), (8, 10), (12, 15)]
Output: Intervals after merging overlapping intervals are [(1, 6), (7, 10), (12, 15)]
Explanation:
(1, 5), (2, 3), (4, 6) are overlapping intervals. After merging we get (1,6)
(7, 8), (8, 10) are overlapping intervals. After merging we get (7,10)
Example:
Input:
a = 1 → 2 → 3 → 4 → 5 → None
k=2
Output: 1 → 4 → 3 → 2 → 5 → None
Input:
a = 1 → 7 → 4 → 5 → None
k=2
Output: 1 → 4 → 7 → 5 → None
[CO1, 10 marks each]